• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ternary operator

 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kicking up dust on the ground
OK, I'll use it, but I don't have to like it.
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic