4 ms·
I think it would be great if there was a way to easily capture my "most common error categories". Years ago I got bit a couple of times by assignment instead o
by jaxn 15y ago
I think it would be great if there was a way to easily capture my "most common error categories".
Years ago I got bit a couple of times by assignment instead of comparison errors (i.e. if(x = 1) instead of if(x == 1))
I noticed a pattern and made a conscious to always put the variable last in comparisons, so for years I have written if(1==x) so that the compile would fail if I wrote if(1=x).
Having a way to see my most common errors would give the opportunity to look for similar error-proofing measures.
- dave1010uk 15y agoI have seen this practice refered to as "Yoda conditionals" but never knew the reason for it. Although it reads wrongly (to me), I can now see the logic. A lot of the code I write is JavaScript in Aptana, which has a real-time JSLint checker that warns against assignments in conditionals.
- jaxn 15y agoI have never heard it called "Yoda conditionals" before. That is awesome.
- fexl 15y agoI always compile C code this way, so it warns me about that particular type of error: gcc -c -Wall -Werror -ansi ... You may not have to use all of those options, I'm not sure. The error message is: "error: suggest parentheses around assignment used as truth value" I find that very helpful.
- Jach 15y agoYou forgot "-pedantic" and "-Wextra". =P (And possibly a few more one-offs that aren't contained in the groups.) Though the key flag there to make it warn is -Wall, so you can still compile with c99 and avoid the -ansi flag. Pretty much any compiler or language worth its salt that allows assignment in conditionals with the equals sign (so Python, Lisp are out) will warn you about it.
- chopsueyar 15y agoThat is quite interesting. So, have you stopped using assignments instead of comparison operators, or does the compiler just prevent you from writing a logic error?