What are yoda conditions?
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 :
- if (value == 42) {
- ...
- }
As yoda condition written:
- if (42 == value) {
- ...
- }
I found some discussions on stackoverflow.com (removed and backuped) and collected some pros and no really contras.
prevent an assignment
- if (value = 42) {
- ...
- }
If you make this typo in javascript the condition is true and value is changed. In C (and many other languages) your compiler/interpreter 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 an object:
- 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 (the example wont work with javascript Array):
- myList = [1, 2, 3]
- if 1 in myList:
- ...
Its not the syntax of yoda conditions but the sequence of values can changed.
- myList = [1, 2, 3]
- if myList.count(1):
- ...
conclusion
yoda says: "Try not. Do or do not, there is no try.". My conclusion is, you should describe it in you coding style guide and use it consistent.
- Login to post comments
Comments
Twitter Trackbacks for What are yoda conditions? | united-c (not verified) - Thu, 06/10/2010 - 16:44
[...] What are yoda conditions? | united-coders.com united-coders.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 [...]
kiran aghor (not verified) - Fri, 06/11/2010 - 05:29
this is kool. :) thanks for the post.
Twitter Trackbacks for What are yoda conditions? | united-c (not verified) - Fri, 06/11/2010 - 11:03
[...] What are yoda conditions? | united-coders.com united-coders.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://united-coders.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://united-coders.com/node/63 ” 8 hours ago retweet Filter tweets [...]
fwolf (not verified) - Thu, 07/01/2010 - 11:28
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.
Google code jam solution for alien numbers | united-coders.c (not verified) - Mon, 09/20/2010 - 19:46
[...] examples as yoda condition from binary, hex or octal into [...]
Astrapi (not verified) - Wed, 06/08/2011 - 13:06
YODA CONDITION are funny but
for the sake of readable code
don't use it!
Like u say “If blue is the sky”.