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.
SaveShareSend
Tags: methodology, php
Comments
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.
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.
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.
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#.
Voters For This Link (13)
Voters Against This Link (0)