Link Details

Every good programmer should constantly be looking for ways to improve the look and readability of their code. One of my favorite ways to reduce vertical length while maintaining readability is to use the lesser-known ternary comparison operator.

Posted by legendx  |   Jun 26 2008 / 09:57

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Comments

User 258461 avatar

EllisGL replied ago:

"Every good programmer should constantly be looking for ways to improve the look and readability of their code." Ah - I find that $myvar = ($x == $y) ? TRUE : FALSE; is a lot less readable.

Reply -1 votes
User 212108 avatar

legendx replied ago:

To each his own. It's very easy to get carried away and lose the readability bonus you get with ternary operators. If you have to think for a second if this is easy to read or not.. you're probably better off with a good ole' if-then statement.

Reply 1 votes
User 200692 avatar

whiskeyjack replied ago:

Love the ternary. Easy to abuse but not that complicated really. It's not exatly "new" since it's from C. Perl's got it, Ruby's Got it. Python has it in the somewhat more verbose form A = C if B else D ( translates to A = B ? C : D ). Just don't put a ridiculously large condition and if you do more than one, format it nicely.

Reply 1 votes
User 281687 avatar

paul_houle replied ago:

Personally I like the ternary operator a lot. This article is of interest to programmers who work in all C-derived languages such as Java and C#.

Reply 1 votes

Recommended Links

Scala
Written by: Ryan Knight
Featured Refcardz: Top Refcardz:
  1. Apache Hadoop
  2. Play
  3. Akka
  4. Debugging JavaScript
  5. Design Patterns
  1. Apache Hadoop
  2. REST
  3. Java
  4. Git
  5. Java Performance
Connect with DZone