Assignment in test: OK?

James C Burley burley at world.std.com
Thu Sep 6 19:44:59 AEST 1990


In article <1990Sep5.185451.25532 at DRD.Com> mark at DRD.Com (Mark Lawrence) writes:

   I have a proclivity to code like the following:

      if( (status = FuncCall(foo, bar, gex)) < SomeValue)
	      GripeAboutIt();

   or variations thereof.  When I run saber on such code, it babbles a 
   warning about the assignment in the test.  Being the gullible naif I am, 
   I worry when a heavy duty tool like Saber complains about something.  Is this 
   practice considered kosher by those in the know?  Why or why not?
   -- 
   mark at DRD.Com uunet!apctrc!drd!mark$B!J%^!<%/!!!&%m!<%l%s%9!K(B

Your code is fine IMHO.  But suppose you had written the following
accidentally:

    if (foo = 0)

This is a common mistake.  My guess is that Saber (which I don't know anything
about) is just trying to get you to look at ifs containing assignments, just
in case you meant to say "if (foo == 0)".  In your particular example, I'd
like to think a somewhat smarter analysis by Saber would prevent it from
worrying, since the assignment expression is a subexpression serving as an
operand for a comparison operator.  But getting a programmer to look at more
things than are real problems is preferable to going the other way; and
perhaps Saber just token-analyzes without building expression trees and
suchlike.

BTW, if I ever write a new C-like language, ":=" will be the assignment
operator, "==" the comparison operator, and "=" totally invalid!  Although
I haven't made that mistake more than twice in the past year, the memories
of debugging (without a lint/Saber available) for hours, just to find a case
of that common mistake, stay with me... (-:

James Craig Burley, Software Craftsperson    burley at world.std.com



More information about the Comp.lang.c mailing list