A similar but arguably more useful version of this is when you have two variables, one of which may be null. Traditionally you issue an if-condition test using the variable user input as the left operand but switching it handles the null case automatically. Ex: Foo SOME_CONSTANT = ; Foo userSuppliedInput = ; // This can raise an null pointer exception if (userSuppliedInput.isEquals(SOME_CONSTANT) { // ... } // This c…
Objective-C is really fun here. Messages to nil are no-ops which return zero, which for booleans results in false. Thus, the conditional works just fine either way: if([userSuppliedInput isEqual: SOME_CONSTANT]) { ... If userSuppliedInput is nil, the isEqual: returns false, and it works. It gets really fun when both might be nil: if([a isEqual: b]) { ... If a and b are both nil, then they're conceptually equal, but i…
Yoda Conditions
31–40 of 83 posts
Re: Yoda Conditions
#32Earlier quoted context omitted.
There is nothing wrong with using equals for assignment, the language designers just have to disallow assignments in if conditions.
One more corner case, instead of designing orthogonal features.
For example, in C the assignment operator evaluates to the value of the right-hand side expression (so `int x = (y = 2);` initializes `x` to 2). But in Rust, the assignment operator always evaluates to `()` (a.k.a. "unit", the empty tuple), and trust me, this is how you want the language to work in the presence of pervasive move semantics and single ownership. Furthermore, nothing in Rust is "truthy": if-expressions accept a boolean and nothing else (this is also, IMO, exactly what you want in every language, but I'm sure others will disagree :P ). So `if x = 2 {` is a compiler error in Rust because `()` can't be coerced to a boolean (to say nothing of the fact that this reassignment would probably be a compiler error anyway because most variables in Rust are immutable). This doesn't require any acrobatics, it's just the natural fallout of how the rest of the language works.
Re: Yoda Conditions
#33Re: Yoda Conditions
#34Earlier quoted context omitted.
One more corner case, instead of designing orthogonal features.
It's not a corner case if you do it right. For example, in C the assignment operator evaluates to the value of the right-hand side expression (so `int x = (y = 2);` initializes `x` to 2). But in Rust, the assignment operator always evaluates to `()` (a.k.a. "unit", the empty tuple), and trust me, this is how you want the language to work in the presence of pervasive move semantics and single ownership. Furthermore, n…
Re: Yoda Conditions
#35Don't do this. In languages where accidental assignment is possible (i.e. writing if(a=b) when you meant if(a==b) ), configure the compiler or linter to emit a warning in this situation. For example in C, GCC will complain about this when compiling with -Wall, which you should be using anyway.
The real lesson is for language designers, who for some reason love using equals for assignment. Lisp got it right a hundred years ago and nobody learned.
In fact, it's not easy to have this kind of bug on your language. C just has it because people wanted to write stuff like `int a = b = 0`.
Re: Yoda Conditions
#36I would say that the phrase "Yoda conditions" more appropriately describes Ruby's alternative form of if-statements: if x > 0: y += x versus y += x if x > 0
save if valid
rather than if valid
save
end
but what I see 9 times out of 10 is things.do each |thing|
foo
bar
baz
end if valid
orreally.long.thing.that.i.try.to.parse.in.my.head if acutally_almost_never_happens
which is harder to read since I read top to bottom / left to right, but the flow is bottom to top and right to left
Re: Yoda Conditions
#37Earlier quoted context omitted.
The real lesson is for language designers, who for some reason love using equals for assignment. Lisp got it right a hundred years ago and nobody learned.
There is nothing wrong with using equals for assignment, the language designers just have to disallow assignments in if conditions.
Re: Yoda Conditions
#38Don't do this. In languages where accidental assignment is possible (i.e. writing if(a=b) when you meant if(a==b) ), configure the compiler or linter to emit a warning in this situation. For example in C, GCC will complain about this when compiling with -Wall, which you should be using anyway.
Do do this if the coding standards for the language/framework you're working in require it. Like WordPress.
Re: Yoda Conditions
#39Don't do this. In languages where accidental assignment is possible (i.e. writing if(a=b) when you meant if(a==b) ), configure the compiler or linter to emit a warning in this situation. For example in C, GCC will complain about this when compiling with -Wall, which you should be using anyway.
Eh, I think it is good in Java in cases where Null input is possible.
Re: Yoda Conditions
#40I would say that the phrase "Yoda conditions" more appropriately describes Ruby's alternative form of if-statements: if x > 0: y += x versus y += x if x > 0
One of the worst parts of Ruby. I can see the argument that it could be better to have save if valid rather than if valid save end but what I see 9 times out of 10 is things.do each |thing| foo bar baz end if valid or really.long.thing.that.i.try.to.parse.in.my.head if acutally_almost_never_happens which is harder to read since I read top to bottom / left to right, but the flow is bottom to top and right to left