Yoda was a great teacher except for his word sequence. For programmer exists the yoda condition :
if (value == 42) {
...
}
As yoda condition written:
if (42 == value) {
...
}
I found some discussions on stackoverflow.com and collected pro and contras.
prevent an assignment
if (value = 42) {
...
}
If you make this typo in javascript the condition is true and value is changed. In C your compiler would give you an warning, as yoda condition this would not work and your compiler will throw a compiler error.
if (42 = value) {
...
}
null checks
if (myString != null && myString.equals(“yes”)) {
...
}
This typical double check in java is tiring and will be shorter as yoda condition:
if (“yes”.equals(myString)) {
...
}
in-operator
With javascript the in-operator can find an attribute in myObject:
myObject = {name: "Christian Harms" };
if ("name" in myObject) {
...
}
The variant using value as first part is ok. A more object oriented variant will ask the container if the attribute is true.
if (myObject["name"]) {
...
}
The same example in python with a list (because the example wont work with javascript Array):
myList = [1, 2, 3]
if 1 in myList:
...
The yoda condition can used with a list method:
myList = [1, 2, 3]
if myList.count(1):
...
conclusion
yoda says: “Try not. Do or do not, there is no try.”
Christian Harms
Latest posts by Christian Harms (see all)
- google code jam 2013 – tic-tac-toe-Tomek solution - April 16, 2013
- Google code jam 2013 – the lawnmower - April 14, 2013
- code puzzles and permutations - April 11, 2013




[...] What are yoda conditions? | unitedcoderscom.appspot.com unitedcoderscom.appspot.com/christian-harms/what-are-yoda-conditions – view page – cached Yoda was a great teacher except for his word sequence. For programmer exists the yoda condition. But was is it? Here a normal if-code-snipplet : Tweets about this link Topsy.Data.Twitter.User['rlaksana'] = {“photo”:”http://a1.twimg.com/profile_images/921069808/young-1_normal.png”,”url”:”http://twitter.com/rlaksana”,”nick”:”rlaksana”}; rlaksana: “What are yoda conditions? – http://su.pr/19QmQZ ” 5 minutes ago retweet Topsy.Data.Twitter.User['allseeingyoda'] = {“photo”:”http://a3.twimg.com/profile_images/495886609/yoda-400x300_normal.jpg”,”url”:”http://twitter.com/allseeingyoda”,”nick”:”allseeingyoda”}; allseeingyodaInfluential: “Hmm, realize I did not that potentially applicable to some parts of Computer Science, my way of speaking is. http://bit.ly/9l8WtC #GeekYoda ” 18 minutes ago retweet Topsy.Data.Twitter.User['yodajstarwars'] = {“photo”:”http://a1.twimg.com/profile_images/713954582/Us_normal.jpg”,”url”:”http://twitter.com/yodajstarwars”,”nick”:”yodajstarwars”}; yodajstarwars: “This is hilarious & possibly pertinent. "Yoda" conditions in JavaScript, though it applies to other languages as well. http://bit.ly/9l8WtC ” 21 minutes ago retweet Topsy.Data.Twitter.User['united_coders'] = {“photo”:”http://a1.twimg.com/profile_images/705668504/qrcode_colorful_normal.png”,”url”:”http://twitter.com/united_coders”,”nick”:”united_coders”}; united_coders: “New post: What are yoda conditions? http://tinyurl.com/32cdppk ” 9 hours ago retweet Filter tweets [...]
this is kool.
thanks for the post.
[...] What are yoda conditions? | unitedcoderscom.appspot.com unitedcoderscom.appspot.com/node/63 – view page – cached Yoda was a great teacher except for his word sequence. For programmer exists the yoda condition. But was is it? Here a normal if-code-snipplet : Tweets about this link Topsy.Data.Twitter.User['huxi'] = {“photo”:”http://a1.twimg.com/profile_images/670989138/logo_kreis_weiss_oben_links_normal.png”,”url”:”http://twitter.com/huxi”,”nick”:”huxi”}; huxiInfluential: “What are yoda conditions? – http://unitedcoderscom.appspot.com/node/63 – #coding #style ” 28 minutes ago retweet Topsy.Data.Twitter.User['ferostar'] = {“photo”:”http://a3.twimg.com/profile_images/799612879/vtr_normal.jpg”,”url”:”http://twitter.com/ferostar”,”nick”:”ferostar”}; ferostar: “Yoda conditions – http://unitedcoderscom.appspot.com/node/63 ” 8 hours ago retweet Filter tweets [...]
hi there,
this once was the recommended way (aka coding rules) for developing themes and plugins for WordPress.
Looks like they changed that back to “normal”, after they saw nobody would care to code like this anyway.
cu, w0lf.
[...] examples as yoda condition from binary, hex or octal into [...]
YODA CONDITION are funny but
for the sake of readable code
don’t use it!
Like u say “If blue is the sky”.
Pingback: Yoda Conditions | Pack 6 – Palo Alto