It avoids that you write var = NULL
. Writing NULL = var
will yield an error. So you're right :-)
its because you cannot assign a value to a constant, so if by mistake you put =
instead of ==
the compiler throws an error, alerting you
That used to be the case, yes. Of course, nowadays almost all compilers warn about assignments in if()
conditions, so the advantage is only there for people who routinely suppress warnings.
That is a common justification, as is the argument that you don't want to push the constant off to the far right of the screen with a long-winded expression. The latter argument has never sounded particularly convincing to me, and the former isn't really valid these days, since any self-respecting compiler will issue warnings (you do compile with warnings-as-errors, don't you? :-).
EDIT: I just came across the new Xcode 4 preview, and just look at the example they chose to exemplify their new "Fix-it" feature!
To catch the difference between assigning and comparing.
If you mean:
if (ptr == foo)
but type
if (ptr = foo)
if will still be valid code, since ptr = foo
will evaluate to a boolean check on ptr
after it has been set to the value of foo
. Obviously, you don't want this.
However, I find it hurts readability considerably, and given that most IDE's and preprocessors will catch this anyway, never use this style.
As a java developer I tend to write expressions like this:
//primitive check
if(constant == variable)
//Object check
if(constant.equals(variable))
This is particularly important for the object because the expression doesn't error if the variable is null. If the expression were in the other order, a null variable would error. I maintain consistency and do the same order for primitives.
Check out the top-rated answer to this question..
http://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined
They've dubbed this coding style "Yoda Conditions".
This has been dubbed as a "Yoda Conditional"!!!
See here http://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined
I really like that term because:
if(Light::On == light)
Reads as:
"If on is the light"
As stated already, this is used to prevent incorrect assignment. It could be argued that this practice is archaic based on modern IDEs but I still think it is good practice.
Catching assignment is a major reason Yoda conditions are used, but there are others: In C++ the operator is invoked on the LHS operand. As constants are typically const
(duh), or primitives, this limits the possibility of non-sensical operations. One example is =
on a primitive literal, such as the common reason given (1 = a
), but this will include operator=() const
, or whatever operator is used for non-POD types.
With a language such as VB 6.0 which does not not have distinct assignment and comparison operators,
a = 2
' Will compile, whether you mean a is assigned 2 or whether you are comparing a with 2 If you meant compare, there is a likelihood that a runtime error ensues.
' For Assignment if you always write
a = 2
' And for Assignment if you always write
2 = a
' You eliminate the Compile-success and runtime-error scenario.
But, this is just the tip: the visual hint is unavailable when you have an expression like
a = b ' Comparison or assignment?
C# has: - different assignment (=) & comparison (==) symbols - comparisons have to be wrapped in brackets.
Then this becomes a non-issue.
You're right - it's to trigger a compiler error if you mistype "==" as "=", since an assignment will always return true. While this will usually be easy to notice and debug, once in a while it will turn into a very hard to detect bug, think of this:
#define OK 2 // Or something...
[...]
while(status = OK){
// This loop will never end
// Even if you change the value of status within it
[...]
}
That can be a nasty bug to find, especially if the block belonging to the offending statement is long (imagine looking for all the possible reasons why status is always staying OK).
If on the other hand you used:
while(OK = status){
That would throw a compiler error, since you cannot assign a value to a constant.
This practice is sometimes referred to as Yoda conditions, since it juxtaposes the object and subject. Like "if status is OK" vs "if OK is status" and "the sky is blue" vs "blue is the sky" - the latter being something Yoda might say.