I remember being told that having an if statement, such as if(10 == $myVariable), is bad practice. However, why is this bad practice and what is its name? (I can vaguely remember that he has a specific name).
if(10 == $myVariable)
According to a message in the blog of Jeff Atwood Horror coding , user the StackOverflow zneak calls this condition Yoda. The description is as follows:
Using if(constant == variable)instead if(variable == constant), for example if(4 == foo). Because it says "if blue is the sky" or "if a tall man."
if(constant == variable)
if(variable == constant)
if(4 == foo)
Yoda, .
exussum , , .
-
if (10 = $myVariable)
,
if ($myVariable = 10)
if true, $myVariable 10
, .
http://en.wikipedia.org/wiki/Yoda_conditions
, null , Java. , , myVariable null:
null
if ("foo".equals(myVariable)) { // Do something }
NullPointerException:
NullPointerException
if (myVariable.equals("foo")) { // Do something }
, , - :
if (myVariable != null && myVariable.equals("foo")) { // Do something }