Why I hate the phrase “Long Running Transactions”… David Geary gets blogging on JSF
Aug 13

No more troubles with ternary

Tech Add comments

I am a fan of the ternary operator. I get bored of if(), and prefer to jump into a ? : if it makes sense.

I think part of the reason that I like it, is that I like the shortcuts in life. I really used to enjoy:

my $foo = $bar || “In case”;

die “Naughty” unless $you_were_good;

One of the things that bugged me about the ternary operator in Java, was that it would catch some people out on items such as:

String str =”whee”;

StringBuilder builder = “whee”;

boolean mutable = true;

CharSequence chars = mutable ? builder : str;

This used to fail all the time, even though both String and StringBuilder (and StringBuffer) implement CharSequence.

Luckily, this now works in JDK 1.5, and we don’t have to resort to the ugly:

CharSequence chars = mutable ? (CharSequence)builder : (CharSequence)str;

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'ajax'

Loading...