Newsgroups: comp.lang.c++
Path: utzoo!henry
From: henry@utzoo.uucp (Henry Spencer)
Subject: Re: Improved switch statement (was Re: goodbye cpp ???)
Message-ID: <1988Dec18.010151.27461@utzoo.uucp>
Organization: U of Toronto Zoology
References: <6590072@hplsla.HP.COM> <1106@etive.ed.ac.uk> <33528@bbn.COM> <1907@ogccse.ogc.edu> <574@redsox.UUCP>
Date: Sun, 18 Dec 88 01:01:51 GMT

In article <574@redsox.UUCP> campbell@redsox.UUCP (Larry Campbell) writes:
>I think this is much clearer and neater and far less prone to error during
>maintenance than the current alternative:
>
>	bool flag1, flag2, flag3;
>	if (flag1)
>	then
>	    // stuff
>	else
>	    if (flag2)
>	    then
>		// stuff
>	    else
>		if (flag3)
>		...

Indenting should reflect the structure of code, not some arbitrary rule
that says that the bodies of ifs should always be indented one stop beyond
the if.  Like this:

	bool flag1, flag2, flag3;
	if (flag1)
	then
		// stuff
	else if (flag2)
	then
		// stuff
	else if (flag3)
	then
		...

This particular situation is common enough that enlightened indenting
standards recognize it as a special case.  More generally, it is sometimes
the case that one can make code clearer by violating a style standard.
(Note, I am not speaking of defining one's own standard, but of a single
violation in otherwise-conforming code.)  This is not something to do
without careful thought, and experienced judgement is required to decide
when it is really in order (i.e., novice programmers should probably be
required to do things "by the book"), but once in a long while it helps.

For example, aside from its tendency to run into the right margin, code
with many indenting levels can be hard to follow because complex nesting
is not easy for the human mind to deal with.  One can sometimes make an
important improvement with a "compound" control structure, e.g.:

	/* NOTE NONSTANDARD INDENTING */
	for (c = getchar(); c != '\n'; c = getchar()) switch (c) {
	case 'a':
		...
	case 'b':
		...
	...
	}

This can be especially significant if there are many cases, meaning that
the beginning and end of the indenting level(s) are far apart, which makes
it particularly hard to match them up.

This sort of thing needs to be done *VERY* sparingly, restricted to places
where it makes a big difference, and commented well.  But it can help.
-- 
"God willing, we will return." |     Henry Spencer at U of Toronto Zoology
-Eugene Cernan, the Moon, 1972 | uunet!attcan!utzoo!henry henry@zoo.toronto.edu
