3 ms·
It isn't just mathematically unsound, it's the cause of many errors, especially in C. Certainly a "million dollar mistake". A classic way to guard against acci
by cup-of-tea 9y ago
It isn't just mathematically unsound, it's the cause of many errors, especially in C. Certainly a "million dollar mistake".
A classic way to guard against accidental use of assignment in logic statements is to write it "backwards", i.e.:
if (2 == x) {...}
Since (2 = x) won't compile, while (x = 2) will not only compile but might appear to work for a very long time.
- jwilk 9y agohttps://en.wikipedia.org/wiki/Yoda_conditions https://en.wikipedia.org/wiki/Yoda_conditions
- v_lisivka 9y agoIt's better to use -Wall -Werror options for C compiler.
- ben509 9y agoAgreed, fix the tools, not the person. Really, any source you have control over should have all error checking possible turned on and breaking immediately, _especially_ with C derivatives.
- cup-of-tea 9y agoThat won't always catch the error and even if it did, why is it better?
- ben509 9y ago-Wall can almost always catch the problem as it will insist on redundant parens surrounding an assignment. So you can only screw it up if you typed extra parens for an equality expression. Variable on left is conventional, it's how people sound it out in their head, so it's just easier to process mentally when the expression becomes more complex. Yoda syntax catches that single error at the expense of making all equality expressions harder to write, read and maintain. And, besides, if you're going to use Yoda syntax, you're going to want your linter to enforce it... at which point you may as well turn on -Wall anyway.
- greenhouse_gas 9y agoAlso how do you prevent "if(a=b)" if both sides are variables?
- ben509 9y agoI will let clang's amazingly detailed error messages speak for themselves: ben@burnination ~ $ cat test.c int main() { int a = 3; int b = 2; if (a=b) { return 1; } else { return 0; } } ben@burnination ~ $ clang -Wall test.c test.c:4:10: warning: using the result of an assignment as a condition without parentheses [-Wparentheses] if (a=b) { ~^~ test.c:4:10: note: place parentheses around the assignment to silence this warning if (a=b) { ^ ( ) test.c:4:10: note: use '==' to turn this assignment into an equality comparison if (a=b) { ^ == 1 warning generated.
- adrianmonk 9y agoIt isn't mathematically unsound. It is just a terminal symbol in a grammar that follows a different (arbitrary) convention than mathematical notation does. And the errors result from typing "=" when you needed "==" has nothing to do with mathematics. It is a language ergonomics issue resulting from two operators being legal in the same context and one being similar to (a prefix of) the other.
- cup-of-tea 9y ago"My writing isn't bad, I just follow different conventions." The equals sign has been used for hundreds of years in mathematics and it's completely unnecessary to change this meaning.
- TheCoelacanth 9y agoThere are hundreds of years of tradition in mathematics to support making up your own definitions of things in whatever way is convenient for the domain you are dealing with. There is no reason to expect programming languages to deviate from that tradition.