Article View: comp.std.c++
Article #3408Re: Article for comp.std.c++: Eliminating #ifdef: if const
From: Fergus Henderson
Date: Thu, 22 Feb 1996 20:20
Date: Thu, 22 Feb 1996 20:20
73 lines
2853 bytes
2853 bytes
shankar@mti.mti.sgi.com (Shankar Unni) writes:
>Often, an #ifdef covers just a small part of an expression that may be
>different between different environments, or some such small differences:
>
> if (some condition) {
>#ifdef SOMEIMPL
> if (some other condition) {
>#endif /* SOMEIMPL */
> /* code */
> /* code */
> /* code */
>#ifdef SOMEIMPL
> }
>#endif /* SOMEIMPL */
> }
But with `if const', your 11 lines of code could be rewritten as either
if (some condition) {
if (SOMEIMPL && some other condition) {
/* code */
/* code */
/* code */
}
}
(7 lines of code)
or, if `some other condition' won't compile on other implementations, as
if (some condition) {
bool some_other_condition = true;
if const (SOMEIMPL) {
some_other_condition = some other condition;
}
if (other_condition) {
/* code */
/* code */
/* code */
}
}
(11 lines of code)
Both of these solutions have the advantage that the test for `SOMEIMPL'
occurs only once, so things will be easier if you later have to change it to
`SOMEIMPL || SOMEOTHERIMPL'.
>Duplicating all the code so that each half is precisely correct in its {}
>matching is both tedious and error-prone (trying to verify that both parts
>of the expression are fixed).
But fortunately as shown above it would not be required.
>Also, the #ifdefs serve a purpose in pointing out the fact that this piece
>of code is system-dependent. The syntax you propose hides its warts rather
>too well, and make it easy for a reader to miss the significant piece of
>information that this area of code is system-dependent.
True, but a convention of using upper-case for SOMEIMPL and similar constants
would make such tests stand out.
--
Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3
---
[ To submit articles: try just posting with your news-reader.
If that fails, use mailto:std-c++@ncar.ucar.edu
FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu.
]
Technical Details
Message-ID:
<4giddp$7q6@mulga.cs.mu.OZ.AU>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<199602160807.IAA06126@condor.ukc.ac.uk> <4gb0a4$sa3@fido.asd.sgi.com>Show Full Headers
Complete Article Headers
From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson) Message-ID: <4giddp$7q6@mulga.cs.mu.OZ.AU> X-Original-Date: 22 Feb 1996 18:42:01 GMT Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 22 Feb 96 20:20:17 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Re: Article for comp.std.c++: Eliminating #ifdef: if const Organization: Comp Sci, University of Melbourne References: <199602160807.IAA06126@condor.ukc.ac.uk> <4gb0a4$sa3@fido.asd.sgi.com> X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMSzQD+EDnX0m9pzZAQH2BQF+MeSYzR9OwhgOR7uS276YQofZUnI+MPPd 6xq5rGngDUvmpiJ6PRuumWtxZpYIMlBB =PKxA