3 ms·
Oh, I didn't realize that had a name! My boss does that, and it is incredibly irritating.
by subwindow 16y ago
Oh, I didn't realize that had a name! My boss does that, and it is incredibly irritating.
- leftnode 16y agoUntil you're system is crippled by a bug as a result of not using them.
- masklinn 16y agoThis is something that cannot happen in Python. Yoda conditions are entirely pointless in that language.
- acangiano 16y agoNot in this case, but Yoda conditions can be useful. The most common case is preventing accidental assignment when you are trying to check for the equality of values. Say that you mean to do: if (something == 2) If you forget an equal sign, the resulting code can lead to subtile bugs: if (something = 2) // Not what we meant If you are using a Yoda condition and forget an equal sign, you'll get an error right away (so that you can correct it): if (2 = something) // Error There are other use cases, particularly related to nil values, but this should show you the gist of it.
- masklinn 16y ago> Not in this case, but Yoda conditions can be useful. Not in Python. Assignments as anything but stand-alone (potentially chained) statements are syntax errors. The parser will not let you write if something = 2: in the first place.
- acangiano 16y agoThat's why I said "not in this case". There are languages affected by this though (typically C-based ones). Ideally, their compiler will give you a warning about it, but that's not always the case.
- moe 16y agoMay be irritating for some but good practice regardless. Putting the constant left in a conditional means you will never miss an accidental = instead of ==.
- masklinn 16y ago> Putting the constant left in a conditional means you will never miss an accidental = instead of ==. That's only useful for dumb languages >>> a = 5 >>> if a = 3: print 'ok'; else: print 'nok' File "<stdin>", line 1 if a = 3: print 'ok'; else: print 'nok' ^ SyntaxError: invalid syntax Python simply does not allow assignments in conditional contexts.
- moe 16y agoDumb is a strong word, young padawan. The difference between theory and practice is smaller in theory than in practice.
- masklinn 16y ago> Dumb is a strong word, young padawan. I carefully weighted my words, and I rest my case.
- 146 16y ago> That's only useful for dumb languages The compilers/interpreters I know that don't do this are PHP and Perl (C/C++ will do it if you use -Wall, which you should be using anyway). So yeah, you're pretty much right. /snark