🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3361

Re: Article for comp.std.c++: Eliminating #ifdef: if const

#3361
From: mtr
Date: Tue, 20 Feb 1996 15:44
72 lines
2698 bytes
Shankar Unni (shankar@engr.sgi.com) wrote:
>No dice.
>
>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 */
>   }

Personally I would be quite happy to see that style of #ifdef'ing
disappear.  I've seen too much code rendered totally impenetrable by
just this technique.  I would write the above as:

	if (some condition) {
		if const (SOMEIMPL) {
			if (some other condition)
				code();
		}
		else const {
			code();
		}
	}

where code() is the code wrapped up in a (possibly inline) function.
Alternatively, if you can persuade your compiler not to complain about
a constant if() controlling expression:

	if (some condition) {
		bool doit;

		if const (SOMEIMPL) {
			doit = some other condition;
		}
		else const {
			doit = true;
		}

		if (doit) {
			code
		}
	}

Of course in reality you would hope to bury all the implementation
dependent stuff behind a portability layer.

>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.

I agree that this is a problem, but I would argue that sprinkling `if
const' (or #ifdef) throughout the main body of the code is bad style,
and thus comes under the `any feature can be misused' getout.  Misuse
like this is easily spotted: `if const' is just as visible to grep as #if.

Mark

[ To submit articles: Try just posting with your newsreader.  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: <199602201003.KAA15992@condor.ukc.ac.uk>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
References: <199602160807.IAA06126@condor.ukc.ac.uk>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: mtr <M.T.Russell@ukc.ac.uk>
Newsgroups: comp.std.c++
Subject: Re: Article for comp.std.c++: Eliminating #ifdef: if const
Date: 20 Feb 1996 15:44:54 GMT
Organization: University of Kent at Canterbury
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <199602201003.KAA15992@condor.ukc.ac.uk>
References: <199602160807.IAA06126@condor.ukc.ac.uk>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
In-Reply-To: <4gb0a4$sa3@fido.asd.sgi.com>
Content-Length: 1734
X-Lines: 65
Originator: clamage@taumet