Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Coding style for small if statements
Date: Thu, 20 Feb 2020 07:18:32 -0800
Organization: A noiseless patient Spider
Lines: 120
Message-ID: <868skxe187.fsf@linuxsc.com>
References: <20200131234613.59047e037939ceb549b8e6d6@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="deb22254a47215e7a717fd925781483c"; logging-data="9062"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18qnvNmVkgOsRp/6U5zV2rlTPDa5RDb2pE="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:8os8askvMQHyqh7b+FPYebEYtho= sha1:EXdvjn9BKYsRQZTvsrKzpqHdE58=
Xref: csiph.com comp.lang.c:150091
Anton Shepelev writes:
> What do you think about the formatting and readability of
> the following code of mine from a display-setup function for
> the DOSBox emulator:
>
> if( dblh != dblw )
> if( dblh ) gfx_flags |= GFX_DBL_H; else
> gfx_flags |= GFX_DBL_W;
>
> If the graphical mode of the DOS application is a double-
> with mode or a double-height mode, but not both, the
> function adds the corresponding flag to the gfx_flags set.
> 320x400, for example, is a double-height mode.
Downthread you give this
> Improved version:
>
> if( dblh != dblw )
> if( dblh ) gfx_flags |= GFX_DBL_H;
> else gfx_flags |= GFX_DBL_W;
I agree the improved version is better than the original. The
'else' at the end of the if line is peculiar; I don't remember
seeing that in C code before. Putting the else under the if
is more usual, and definitely better.
The if/else with the controlled assignments on the same lines as
the if and the else is good (and IMO better than writing these
assignments on separate lines, either with braces or without).
Writing the controlled assignments on the same lines as if and
else makes it easier to see the parallel construction of the
two controlled statements.
With regard to the outer 'if( dblh != dblw )', my own pattern
would use braces to the controlled (and indented) if/else.
This practice applies generally to if, else, do, while, for
(and also switch, though that almost never comes up): either
a single statement on the same line, with no braces, or use
braces and indent for controlled statement(s) on subsequent
lines, regardless of whether the controlled block is a single
statement or multiple statements.
(The question of where the braces should go is a heated topic
which I defer for now, possibly to return to it later.)
Having said that, if someone wanted to write this example
without using braces, I would suggest and recommend greater
indentation for the controlled if/else statement:
if( dblh != dblw )
if( dblh ) gfx_flags |= GFX_DBL_H;
else gfx_flags |= GFX_DBL_W;
or even this:
if( dblh != dblw )
if( dblh ) gfx_flags |= GFX_DBL_H;
else gfx_flags |= GFX_DBL_W;
Especially for the second writing, there should be little doubt
as to what meaning the writer intended.
> The criticism I received is that the reader may not eaily
> decide whether the author meant
>
> if (dblh != dblw) {
> if (dblh)
> gfx_flags |= GFX_DBL_H;
> } else {
> gfx_flags |= GFX_DBL_W;
> }
>
> or
>
> if (dblh != dblw) {
> if (dblh)
> gfx_flags |= GFX_DBL_H;
> else
> gfx_flags |= GFX_DBL_W;
> }
I am just slightly sympathetic to this criticism. There isn't
any question how the C language parses the original statement,
but it's easy to imagine that the writer somehow missed a space
or two and may have wanted to express a different meaning. If
you try showing the revised versions with more indentation, what
does your critic say? Is there just as much doubt for the
version with eight spaces of indentation? I'm curious to hear
what they would say.
> In addition to that, GCC emits a warning about a dangling
> `else', whereas in my option the indentation makes the
> perfectly clear while keeping it concise: the outer test of
> dblh != dblw acts upon the inner if-else operator as a
> whole.
I don't completely disagree, although I repeat my suggestion for
greater indentation. I believe though that using braces on the
outer if is the more common practice. If we add to that the
observation that GCC gives a warning, I think that's a pretty
strong indicator that using braces on the outer if is a better
choice here.
> I do not consier ?: as redundant and unintuitive. My friend,
> a talented programmer and mathematician, initially thought
> that the second argument was used in case the first was false,
> because of the ordering of truth values in ascending order.
> Nothing in the syntax hints at the implied analogy with the
> if-then-else statement.
I defer comments (if any) on this question to a subsequent posting.
> What are your opinions?
I thought it would be better to offer relatively more in the way
of suggestions and observations, and relatively less in the way
of my own opinions, and that is what I have tried to do above.