Link Details

Link 89822 thumbnail
User 212108 avatar

By legendx
via mikebernat.com
Published: Jun 26 2008 / 09:57

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.
  • 13
  • 1
  • 2792
  • 0

Comments

Add your comment
User 258461 avatar

EllisGL replied ago:

-1 votes Vote down Vote up Reply

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

User 212108 avatar

legendx replied ago:

1 votes Vote down Vote up Reply

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.

User 200692 avatar

whiskeyjack replied ago:

1 votes Vote down Vote up Reply

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.

User 281687 avatar

paul_houle replied ago:

1 votes Vote down Vote up Reply

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

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.

Java Performance Optimization
Written by: Pierre-Hugues Charbonneau
Featured Refcardz: Top Refcardz:
  1. Design Patterns
  2. OO JS
  3. Cont. Delivery
  4. Java EE7
  5. HTML5 Mobile
  1. Node.js
  2. Debugging JavaScript
  3. OO JS
  4. JSON
  5. Ajax