if - else

Michael Geary Mike at DeleteThis.Geary.com
Sat Nov 29 03:16:35 EST 2003


> > | if (10 == sum) {...}
> > |
> > | rather than
> > |
> > | if (sum == 10) {...}

> > Yes, there's such a style, and it's considered by experienced C
> > programmers to be as weird as if( (a<b) == TRUE )...

> Dunno about that, I've been using C since 1986 and every C
> project I've worked on since 1991 (about half a dozen biggish
> ones) has mandated the
>
> if (CONST == variable)...
>
> style of if statement check, and most of the recent books
> recommend it. So an awful lot of experienced C
> programmers use it day in day out - and I haven't heard
> any loud complaints.
> Generally anything that saves bugs in C is "A Good Thing",
> its like running lint - you just build it into the make rules
> so that you never forget...

The problem with "if( CONST == variable )" is that it reads unnaturally,
creating a mental speed bump when someone reads your code. Admittedly it's a
fairly minor speed bump, and avoiding coding errors is a good thing.
However, this contrivance is unnecessary with modern C compilers, which
issue a warning if you code "if( variable = CONST )".

With Microsoft Visual C++, I always set my project options so that warnings
are errors. If I code "if( variable = CONST )", it won't compile. Thus, I'm
able to use the more natural "if( variable == CONST )" notation without fear
of error.

It's certainly pleasant that Python sidesteps this issue completely! :-)

-Mike






More information about the Python-list mailing list