File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Cattle Drive and the fly likes ternary operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Storm Applied this week in the Other Open Source APIs forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "ternary operator" Watch "ternary operator" New topic

ternary operator

Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
One of the nitpics that I received was a suggestion to replace some of my code with the ternary operator. Is this necessary? I truly hate the ternary operator because I think it is less readable and it certainly is less expandable.
[ January 11, 2002: Message edited by: Matthew Phillips ]

Matthew Phillips
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9056
About 95% of the time I would say that use of the ternary operator does not add readability, but there are cases where it does add readability, and one of the main things I try to do in the cattle drive is teach readability. Yes, I know I'm being redundant.

Here is a link where we discussed it previously. In particular, I'd like to point you to the last post in that thread.
[ January 11, 2002: Message edited by: Marilyn deQueiroz ]

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
I used to avoid it myself because it looked "strange", but recently I've become fond of it's ability to streamline certain idioms, such as
if (someObjRef == null )
foo = 0;
foo = someObjRef.someMethod();
foo = (someObjRef == null ? 0 : someObjRef.someMethod());
There are other such idioms that escape me at the moment, but I've re-thought my aversion to the ternary operator and am putting it to good use now.


SCJP 1.4
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
kicking up dust on the ground
OK, I'll use it, but I don't have to like it.
Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
I was just reading the sun coding style guide today and I saw there are 3 possibilities for the ternary operator expressions. One of them looks very readable:
( just add a pinch of spacing )

[ January 12, 2002: Message edited by: Terence Doyle ]
[ January 12, 2002: Message edited by: Terence Doyle ]

Raising Flares debut album 'Ignition' out now

Terry Doyle <br />SCPJ 1.4 , SCWCD , SCMAD(Beta)
I agree. Here's the link:
subject: ternary operator