What are yoda conditions?

Christian Harms's picture

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 :

  1. if (value == 42) {
  2.     ...
  3. }

As yoda condition written:

  1. if (42 == value) {
  2.     ...
  3. }

I found some discussions on stackoverflow.com (removed and backuped) and collected some pros and no really contras.

prevent an assignment

  1. if (value = 42) {
  2.    ...
  3. }

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.

  1. if (42 = value) {
  2.     ...
  3. }

null checks

  1. if (myString != null && myString.equals(“yes”)) {
  2.     ...
  3. }

This typical double check in java is tiring and will be shorter as yoda condition:

  1. if (“yes”.equals(myString)) {
  2.    ...
  3. }

in-operator

With javascript the in-operator can find an attribute in an object:

  1. myObject = {name: "Christian Harms" };
  2. if ("name" in myObject) {
  3.     ...
  4. }

The variant using value as first part is ok. A more object oriented variant will ask the container if the attribute is true.

  1. if (myObject["name"]) {
  2.     ...
  3. }

The same example in python with a list (the example wont work with javascript Array):

  1. myList = [1, 2, 3]
  2. if 1 in myList:
  3.     ...

Its not the syntax of yoda conditions but the sequence of values can changed.

  1. myList = [1, 2, 3]
  2. if myList.count(1):
  3.     ...

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.

"What are yoda conditions?"

What are yoda conditions?

Comments

 Twitter Trackbacks for What are yoda conditions? | united-c's picture

[...] 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's picture

this is kool. :) thanks for the post.

 Twitter Trackbacks for What are yoda conditions? | united-c's picture

[...] 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's picture

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's picture

[...] examples as yoda condition from binary, hex or octal into [...]

Astrapi's picture

YODA CONDITION are funny but
for the sake of readable code
don't use it!
Like u say “If blue is the sky”.