DEV Community

Discussion on: What programming best practice do you disagree with?

Collapse
 
oscherler profile image
Olivier “Ölbaum” Scherler

Yoda conditions. A lot of PHP projects write if(42 == $value) instead of if($value == 42), to avoid the mistake of forgetting an equal sign and getting an if($value = 42) statement with an assignment in it, that thus will alway be true.

Except that in PHP, you need to write if(42 === $value), otherwise you are vulnerable to shady automatic type conversions. And those projects also write if(42 !== $value), if(42 < $value), etc, for symmetry, even though there are no mistakes to avoid in these cases. The result is code that is annoying to read, and you have to train yourself to not forget to reverse the operands, when you already trained yourself to not forget the third equal sign. It simply has no benefit.

Collapse
 
talha131 profile image
Talha Mansoor

A lot of C code, that I have come across, also has this issue, and for the same reason, "to avoid the mistake of forgetting an equal sign".