if - else
Jay O'Connor
joconnor at cybermesa.com
Sat Nov 29 09:18:06 EST 2003
Michael Geary wrote:
>>>| 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 )".
>
>
I was on a TCL project (ugh..one reason I don't use TKinter is simply
guilt by association...I learned to hate TCL on that project) where I
inherited code from a developer who did a lot of that "if 10==x" kinda
stuff. Basically we said the same thing; "it doesn't read naturally"
> It's certainly pleasant that Python sidesteps this issue completely! :-)
True, but I'd still rather allow the assignment and catch the error by
not allowing boolean comparison of non-boolean values. This would break
a lot of other basic stuff in Python, though, such as using "if seq:" to
determine if a sequence is empty, but I've never been a big fan of that
shortcut (althuogh I use it myself). I'd rather the code have to be a
bit more explicit.
More information about the Python-list
mailing list