5 ms·
There is nothing incorrect or dubious in C about an expression like if (foo = bar), so it wasn't considered a reason to warn the user about for a long time, des
by weland 13y ago
There is nothing incorrect or dubious in C about an expression like if (foo = bar), so it wasn't considered a reason to warn the user about for a long time, despite being a rather common culprit for bugs. Compilers popping up warnings about that is a fairly recent event.
- goldenkey 13y agoPretty sure that's been a warning for a long long time in most compilers. They require you to wrap the assignment to suppress it, that is: if ( (foo=bar) ) Anyho, it really depends on what your -W level is set to. It's good practice to use "-Wall"
- tubs 13y ago-Wall doesn't give all errors though, you probably want -Wall -Wextra.
- chris_wot 13y agoThat's still stupid. Put the assignment on its own line.
- goldenkey 13y agoWow, what an insightful comment.
- chris_wot 13y agoLikewise.
- goldenkey 13y agoI hope you're being sarcastic!
- chris_wot 13y agoUnder what circumstances would you place an assignment into an if statement condition?
- goldenkey 13y agoI like doing it in certain cases. It definitely helps in a while loop when you are working with a stream, iterator, or stack and pulling items off while it isn't empty. Not really sure about the if, it's just kind of convenient sometimes. Guess it all comes down to preference. Too much use and it becomes very unreadable.
- chris_wot 13y agoYou just gave me an example of a loop. In what circumstances do you find it convenient, and when do you feel that the advantages of convenience outweigh that of code readability?
- goldenkey 13y agoIf it's a single assignment, then it's merely a preference. I cannot think of a case where it is explicitly better for a single conditional. However, a compound conditional can actually be more clear if the assignment is there in many cases. For example: if ( (elt = db.getNextResult) && elt.shouldProcess){ ... ... } In a compound conditional, the assignment is forced to be wrapped in parenthesis, so the ambiguity doesn't exist. Anyho, that's that.
- jomar 13y agoSure, if by "fairly recent" you mean at least more than twenty years ago: http://gcc.gnu.org/viewcvs/gcc?view=revision&revision=1853 http://gcc.gnu.org/viewcvs/gcc?view=revision&revision=1853
- csmuk 13y agoThat's GCC. Some of us were blessed with commercial compilers.
- weland 13y agoGCC isn't the only compiler on the planet :-). Other compilers either started doing it more recently, or not at all. The construct it most often warns about is fairly idiomatic on Unix platforms, but not everywhere, so a lot of vendors were not quite as bothered about it.
- jomar 13y agoGCC is indeed not the only compiler around, which is why I said "at least" :-) Even if you're stuck with some other compiler for production builds, it's handy to be compiling regularly with a variety of compilers so as to benefit from the union of the diagnostic abilities of all of them. The construct is a useful idiom, but oftentimes there would be or ought to be a local coding standard recommending that you make what you're doing explicit by comparing the assignment result to the appropriate flavour of zero: if ((foo = bar) != NULL) Now everybody can see what's going on, the warning doesn't arise, and no pedants can complain about the theoretically-unnecesary parentheses in `if ((foo = bar))`.
- weland 13y ago> GCC is indeed not the only compiler around, which is why I said "at least" :-) Even if you're stuck with some other compiler for production builds, it's handy to be compiling regularly with a variety of compilers so as to benefit from the union of the diagnostic abilities of all of them. In my case, at least one of the compilers in question was for the 8051, the platform that won't fucking die. Due to the peculiarities of the aforementioned piece of silicon crap, the compiler was not entirely ANSI-compliant (various #pragmas were actually required to produce working code, indicating various parameters related to memory organisation and the such), thus making it rather difficult to compile the codebase with anything but that particular compiler. > The construct is a useful idiom, but oftentimes there would be or ought to be a local coding standard recommending that you make what you're doing explicit by comparing the assignment result to the appropriate flavour of zero This is the solution I adhere to as well.