Live data from Hacker News

Yoda Conditions

en.wikipedia.org

21–30 of 83 posts

Re: Yoda Conditions

#21
post #3

Don'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.

I think it is better than if(x!=null && x.equals("y")

You have to take bits of concision where you can find them in Java.

Re: Yoda Conditions

#22
I 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

Re: Yoda Conditions

#23
One of the best bugs I've ever seen was in the game Dungeon Crawl Stone Soup. It's a roguelike, and a venerable old one at that, so the codebase is quite tangled and convoluted, with logic for everything in every corner. One of the worshippable gods in the game is Xom, god of chaos, whose effects are just as likely to kill their worshipers as to help them (frustrating for those who might actually want to win, but always good for a laugh). And somewhere in the main game loop was some bit of logic that was only ever supposed to activate while the player was actively worshiping Xom. Guess how this was accidentally implemented:

  if (you.religion = GOD_XOM) {
This actually made it to the live test server, where player save files are automatically upgraded to the most recent build, so anyone logging in immediately found themselves infallibly worshiping the god of chaos, every single tick of in-game time (renouncement is futile). Chaos indeed!

Re: Yoda Conditions

#25
post #20
post #16

Earlier 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.

One more corner case, instead of designing orthogonal features.

Re: Yoda Conditions

#26
post #3

Don'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.

There are useful cases for this. It's much less ambiguous in those cases if your language allows the definition of the variable in the same location, and scopes it. For example, Perl:

    use strict;
    use warnings;
    
    sub one { 1 }
    
    if ( my $one = one() ) {
        say $one; # prints 1
    }
    
    say $one; # compilation error, "Global symbol "$one" requires explicit package name"
This case is trivial, but when you want a temporary variable and don't want to clutter your scope, it can be useful.

That's not to say assignment and equivalence might not be better off with different operators, as noted by others here.

Re: Yoda Conditions

#27
post #3

Don'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.

Exactly, and use 'if ((a = b)) ...' in those rare cases when it's appropriate.

Even then I would prefer an explicit comparison:

    if ((a = b) != NULL) ...
I think Rust has a happy medium, where assignment evaluates to (), not the variable (important as this means you don't have an implicit borrow), but the idiom expressed here is usually replaced by:

    if let Some(thing) = fn_that_returns_option_thing() ...

Re: Yoda Conditions

#28
post #20
post #16

Earlier 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.

It's a little weird. lvalues and rvalues are different things so a biased symbol feels nice to me, <-, e.g.

Re: Yoda Conditions

#29

In PHP I like using Yoda Conditions because there's a common idiom of testing assignment in the conditional: if ($value = getSomeValue()) { // Safely use value } Yoda Conditions defend nicely against accidents when '=' and '==' can be used legally this way and honestly you get used to reading them pretty quick.

I've been coding for decades and I still find them jarring.

They're also a false sense of security as they won't catch:

    if $(value = $someOthervalue) ...

Re: Yoda Conditions

#30
I ran across a bug at $dayjob where someone was clearly trying to use Yoda conditions, but messed up differently:

    if "start" == argv[1]:
        ....
    else if "stop" == argv[1]:
        ....
    else if "reload":
        ....
    else if "status" == argv[1]:
        ....
I think it'd be harder to make this mistake with the conditions the right way around. (Also, never mind that the equality bug isn't even possible in Python.)
Post reply on HN