• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ternary operator

 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kicking up dust on the ground
OK, I'll use it, but I don't have to like it.
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic