Skip navigation.
 
mlRe: assignment in if clause
FROM : Scott Ribe
DATE : Mon May 26 21:35:30 2008

> I disagree with the whole notion that using the result of an assignment
> is "bad style" or "confusing".


I agree that it is perfectly clear. But there is still a serious problem
with it: it is too easy to make the typo of if(a=b) when one intended
if(a==b). This can be a surprisingly difficult typo to spot, when one is
staring at the code with the preconceived belief that one typed if(a==b).

There is no way for a compiler to warn one only on those instances where one
did *not* intend to type if(a=b), and that is why many people consider
if(a=b), if not actually "bad style", at least not worth the risk. I like
if(a=b), it's very C-like, as is if(myPtr). But I don't use it, because even
though I don't do that typo very often, it really just isn't worth it to not
be explicit.

The extra parentheses are just a convention that was adopted as a way to
tell the compiler that you really did want to make an assignment and test
the result in one expression. (I personally hate it, because I keep looking
for the "other stuff that should be in the parentheses"; I prefer just going
ahead and comparing to nil/NULL/0 as appropriate.)

> ...man serving machine, not the other way around as it should be.


Uhm, excuse me? I thought we were talking about C programming? Why would
there be any question about who is the master in this scenario?

;-)

--
Scott Ribe
<email_removed>
http://www.killerbytes.com/
(303) 722-0567 voice

Related mailsAuthorDate
mlalternate pattern for object initializers Stuart Malin May 26, 07:47
mlRe: alternate pattern for object initializers Kyle Sluder May 26, 08:51
mlRe: alternate pattern for object initializers Stuart Malin May 26, 09:25
mlRe: alternate pattern for object initializers Kevin Grant May 26, 09:28
mlRe: alternate pattern for object initializers Michael Gardner May 26, 09:59
mlRe: alternate pattern for object initializers Hamish Allan May 26, 11:01
mlRe: alternate pattern for object initializers Jens Alfke May 26, 19:13
mlassignment in if clause (was: alternate pattern for object initializers) Stuart Malin May 26, 20:06
mlRe: assignment in if clause (was: alternate pattern for object initializers) Hamish Allan May 26, 20:40
mlRe: assignment in if clause (was: alternate pattern for object initializers) Jonathan Prescott May 26, 20:49
mlRe: assignment in if clause Michael Gardner May 26, 21:12
mlRe: assignment in if clause Scott Ribe May 26, 21:35
mlRe: assignment in if clause Hamish Allan May 26, 21:38
mlRe: assignment in if clause Kevin Grant May 27, 05:37
mlRe: assignment in if clause Scott Ribe May 27, 15:46
mlRe: assignment in if clause Steve Christensen May 27, 16:52
mlRe: assignment in if clause Isak Styf May 27, 21:31