aspose file tools*
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "ternary operator" Watch "ternary operator" New topic
Author

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
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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 ]

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

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;
else
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.

Rob


Rob
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
Hi,
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 )


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

Raising Flares debut album 'Ignition' out now

http://www.raisingflares.com

Terry Doyle <br />SCPJ 1.4 , SCWCD , SCMAD(Beta)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ternary operator