Published: Jun 26 2008 / 09:57
"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.
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.
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.
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#.
Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.