united-coders

TwitterFacebookGoogleRSS
  • Home
  • Authors
    • Christian Harms
    • Nico Heid
  • Newsletter
Home » What are yoda conditions?

What are yoda conditions?

Posted on June 9, 2010 by Christian Harms Posted in Uncategorized 6 Comments

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

  • Bio
  • Latest Posts
My Google+ profile

Christian Harms

I am working as a software developer and project manager in germany. After my graduate computer science I started in 2000 as a developer for a warehouse management system. my python knowledge grow up as a team member for one of the TOP3 german portal and freemail services. 2012 switched to the automotive industry to share my knowledge about open source and agile development. In spare time I submit to some coding contests and interesed in smaller developments.

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
« Are you producing banana software?
Google Code Jam – The Snapper Chain »

6 thoughts on “What are yoda conditions?”

  1. Twitter Trackbacks for What are yoda conditions? | united-c says:
    June 10, 2010 at 17:44

    [...] 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 [...]

  2. kiran aghor says:
    June 11, 2010 at 06:29

    this is kool. :) thanks for the post.

  3. Twitter Trackbacks for What are yoda conditions? | united-c says:
    June 11, 2010 at 12:03

    [...] 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 [...]

  4. fwolf says:
    July 1, 2010 at 12: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.

  5. Google code jam solution for alien numbers | united-coders.c says:
    September 20, 2010 at 20:46

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

  6. Astrapi says:
    June 8, 2011 at 14:06

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

Leave a comment Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tags

android code jam code puzzle hackercup hosting java javascript permutations project euler python server

Recent Comments

  • Nico Heid on The art of escaping
  • hardik on Use Android ActivityGroup within TabHost to show different Activity
  • james on Use Android ActivityGroup within TabHost to show different Activity
  • Matthias Reuter on The art of escaping
  • Nico Heid on Show ProgressBar in Notification Area like Google does when downloading from Android market

Recent Posts

  • A scalable, affordable WordPress hosting, lessons learned
  • google code jam 2013 – tic-tac-toe-Tomek solution
  • Google code jam 2013 – the lawnmower
  • code puzzles and permutations
  • Code Jam – Candy Splitting

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Copyright

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
© united-coders
  • 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