2

While using an 'if' statement to check whether a variable is an empty string or not we can write it in two ways i.e. if('' == $variable) and if($variable == ''). I want to know what is the impact of above in different cases?

Thanks in advance!!

  • equality is symmetric. hence, the two ifs are the same thing. – devnull Sep 18 '14 at 04:48
  • 1
    @devnull While technically true, there is quite a bit of variance in C/C++ standards as to which is better because of the possibility of silent typos. – Chris Pitman Sep 18 '14 at 05:13
  • In addition to the answers given, writing if ('constant' == $variable) can also eliminate bugs. If you mistakenly write (a = true) instead of (a == true) you've introduced a bug in your code. (true = a) will give a compiler error. – Dennis_E Sep 18 '14 at 08:40
  • both are wrong, the correct formulation is (a) – kevin cline Sep 18 '14 at 09:07
  • Just wanted to add that having the constant first in the comparison is referred to as a Yoda condition (or notation apparently): > Using if(constant == variable) instead of if(variable == constant), > like if(4 == foo). Because it's like saying "if blue is the sky" or > "if tall is the man". [Coding Horror New Programming Jargon](http://blog.codinghorror.com/new-programming-jargon/) [Wikipedia Yoda Conditions](http://en.wikipedia.org/wiki/Yoda_conditions) (Sorry I would rather have added this as a comment but I don't have the reputation) – Paddy Sep 18 '14 at 08:53
  • @devnull: not in OO. In an object-oriented language, messages are sent to a distinguished object, the *receiver*, and the receiver gets to decide what to do with the message. Reverse the equality and the message gets sent to the other object, which may or may not decide to do something different. There really is no way to implement symmetric methods in OO without express cooperation of all objects involved. You could implement equality as a message send to a context object, i.e. `a == b` is equivalent to `context.==(a, b)` instead of `a.==(b)`, but then it falls upon the context object to … – Jörg W Mittag Sep 18 '14 at 19:52
  • … guarantee symmetry, and again, the object can do with the message as it pleases. – Jörg W Mittag Sep 18 '14 at 19:53

2 Answers2

12

In a modern language, you should be writing your conditions of the form if($variable == ""). This makes the condition easier to read for natural English speakers.

In a legacy language, it sometimes was considered good practice to use the form if("" == $variable) as if you used the more natural form it was possible to create compilable and runnable code which was bugged if you accidentally missed one of the '=' symbols.

Now, most compilers, even for these older languages, will at least warn you if you accidentally miss the '=' symbol.

TL;DR; - use if($variable == "")

Stephen
  • 8,800
  • 3
  • 30
  • 43
  • 4
    The line between modern/legacy languages isn't all that clear. For example in C (a legacy language?), you can have integers in if statement (anything not a 0 is "true"), so the `=` vs. `==` bug is a real concern. But even in Java, if you have `boolean`s, both `if(a=false)` and `if(a==false)` are legal, but the result is different! (And at least my compiler doesn't warn about this case - why would it, because either case is perfectly correct code.) IMO that's a good reason to use `if(""==$variable)` exclusively. – Joonas Pulakka Sep 18 '14 at 06:36
  • 1
    @JoonasPulakka Decent C & C++ compilers warn about assignments in `if` conditions, precisely because it's a common source of error. You normally silence the warning by parenthesising the assignment - it's a way to signal to the compiler "yes, I know what I'm doing." – Angew is no longer proud of SO Sep 18 '14 at 07:00
  • @Angew Funny how you seem to suggest that the Java compiler is not 'decent' :) – marczellm Sep 18 '14 at 08:35
  • 1
    @marczellm In some way, yes. I feel that "perfectly valid, but most probably erroneous" code warrants a warning, if the compiler also provides a simple way of overriding it. – Angew is no longer proud of SO Sep 18 '14 at 08:38
6

This highly depends on the language used!

If == is implemented as a method in an object oriented language then the construct if ($variable == "") could lead to an exception because the object ($variable) might not be initialized or even null.

If you reverse the expression (if ("" == $variable)) then you can be sure that the object acted on ("" is always initialized and never null).

As an example in Java (where the method is called .equals()):

string.equals("")

can cause a NullPointerException because the object string may be null.

"".equals(string)

cannot lead to a NullPointerException as the object "" is never null.

If == is not a method of an object then it does not matter which order is used.

I think that many programmers with such a background use the expression if ("" == $variable) because they are more familiar with it.

Uwe Plonus
  • 1,310
  • 9
  • 16
  • I don't use Java much, and I never thought about `"".equals`. Thanks for the tip :). – TMH Sep 18 '14 at 09:23