99

Is it just me or am I the only one who gets pissed if someone checks the expected result of a variable first?

For example:
if(true === $var)

Instead of:
if($var === true)

Comments
  • 7
    It hurts...
  • 2
    You know why people do that, right?
  • 1
    @Gauthier no clue at all
  • 11
    @Gauthier to avoid the risk of assigning true to $var by mistake? If yes, it's still stupid.
  • 5
    @dennie170 I'm not sure about this language, but in C it's about preventing accidental assignments:
    if ( a = 1 )
    is legal, but probably not what you want.
    if ( 1 = a )
    fails earlier, at compile time.

    Still, your editor and compiler should warn you about the suspicious assignment.
  • 33
    If ($var) is the only true way to write it
  • 7
    @JS96 I don't care too much for it either and don't write it myself, but "stupid" is a bit strong don't you think? It's really only about getting used to it.
    Btw, these are called Yoda conditionals :D
    (but yeah, -Wall -Werror, I prefer)
  • 0
    It doesn't make sense but that is the convention and you must honour that.
  • 7
    @Gauthier I mean that a developer should know exactly the code he's writing, not just write some random code because he must to do that.
    If he wrote "if ($var = true)" because he was distracted, he deserves to lose hours to find that bug.
  • 8
    @JS96 By that same reasoning a developer should never make unit tests. Or test the code manually. Or let someone review it. Or use a linter. Or check that it compiles before pushing the commits.
  • 5
    Ah master Yoda. I see you have come to visit our code?
  • 1
    In Java, writing something like "stuff".equals(var) instead of var.equals("stuff") means avoiding NullPointerException, if var is null.

    But yeah, I hate is as well, and I just do another condition first, for null, as well.
  • 1
    It burns
  • 1
    @epse was about so say something similar 😁
  • 1
    This cleared a lot up for me
  • 0
    @Cruiser What if $var can be true or 1 or "1", etc, and you're working in PHP? 😂
  • 0
    Ah. I see that you did good old Yoda typing... :D
    Now don't do it ever again. Most good IDEs nowadays will detect those types of errors and also people mostly stopped making these mistakes. There is no need to do Yoda typing anymore. :)
  • 0
    Just don't check the assignment to avoid confusion. Let the compiler do it with if($var)
  • 0
    Constants on the left. Its a hangover from the C days. It makes complete sense to do it in terms of readability and avoiding assigning instead of comparison, but I still hate it.
Add Comment