Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's better to use -Wall -Werror options for C compiler.


Agreed, 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.


That won't always catch the error and even if it did, why is it better?


-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.


Also how do you prevent "if(a=b)" if both sides are variables?


I 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: