Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
JS96185348y@Gauthier to avoid the risk of assigning true to $var by mistake? If yes, it's still stupid.
-
@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. -
@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) -
JS96185348y@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. -
Henrik1558y@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.
-
Scanner5568yIn 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. -
@Cruiser What if $var can be true or 1 or "1", etc, and you're working in PHP? 😂
-
arekxv10548yAh. 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. :) -
johnDoe32338yJust don't check the assignment to avoid confusion. Let the compiler do it with if($var)
-
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.
Related Rants
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)
undefined
pissed
annoyed
if
programming
check