Curious assignment behaviour

Paul Wright -$Paul$- at verence.demon.co.uk
Wed Oct 10 06:14:12 EDT 2001


In article <mailman.1002693010.16254.python-list at python.org>,
Tim Peters <tim.one at home.com> wrote:
>[Paul Rubin, on an expression assignment operator]
>> Why on earth does it have to be different from the statement assignment
>> operator?  What's wrong with
>>
>>   while x=get_next(): whatever(x)
>>
>> I just don't buy the rationale that using = instead of == is a big source
>> of bugs.  In 25 years of hacking C code, I think I've seen that error
>> maybe once or twice.
>
>I suggest you're highly atypical here, Paul.  This was the very first of the
>gotchas listed in Andrew Koenig's classic (late 80s) "C Traps and Pitfalls",
>and has made nearly every list of C "deadly sins" I've seen since then.
>I've wasted weeks of my own life helping people track this error down in C,
>too often under extreme time or customer pressure.  The experience of the
>other folks at PythonLabs is similar, so even if it's a disease you're
>immune to, you can be sure this will never go in.

If you're after some quantitative data, Les Hatton[0] lists faults where
"control expression is an assignment" as occurring in 1:3306 lines of C
code in his test population.  The practice of doing assignment in
control expressions is banned in safer subsets of C [1].

While I agree it can be annoying to have to write an extra line
occasionally, I think that the benefits outweigh the costs.

[0] Hatton, L, "Safer C: Developing Software for High-integrity and
Safety-critical systems", McGraw-Hill 1994.

[1] eg. The Motor Industry Software Reliability Association's C
Guidelines, http://www.misra.org.uk/

-- 
----- Paul Wright ------| "Their little anoraks bobbed and danced, their
-paul.wright at pobox.com--| cycling helmets swung with gay abandon - the NatSci
http://pobox.com/~pw201 | Elves were abroad!" -Simon Pick



More information about the Python-list mailing list