• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

? ternary operator

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I recently happen to use ternary operator while implementing some logic in java

for example
boolean flag = false;

if(){
flag = false;
}
...

x= flag?2:3;

where x will be set 2 or 3 depending on the flag true or false;

I got review feedback from one of my team member that ternary operator in java is considered seriously poor programming style if one is not coming from c background. I am not sure that is true.

would like to seek your opinion

 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hogwash. I use it all the time for conditional assignment.

I would not use it for complex functionality or to call methods conditionally. For that, I would use if/else.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always figure the rule is 'whatever makes the code easiest to read' is the correct way to go. For something as simple as this, i would consider it perfectly acceptable.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yeah. There does seem to be an anti ternary operator group out there... and to those people, I agree with Bear when I also say "hogwash".

Henry
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ternary operator is perfect when you have a variable that can be 1 of 2 values. I think once you start nesting ternary's inside each other is when you've crossed the line into maintenance hell.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My airing of grievances against the ternary:

1) I dislike it when the 'flag' is generated for the sole purpose of using the ternary -
bad:

better?:


2) When the flag is lazily named (because it is only going to be used as a flag after all). I would much rather see the conditions in the ternary, or a well named flag. (I don't know why but it seems people are more likely to give bad names to variables used in ternary operators than other places in code, and in my opinion it should be the opposite)
bad:

better?:

or:


Mind you I don't have anything against the ternary operator itself, but these are things I see that irk me...
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I only use it like in your final example. I rarely ever declare a variable, flag, or whatever before use.... I don't like variables I don't need hanging around trying to trip me up.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, using an if to use the ternary to avoid an if is somewhat iffy.

I like to apply common sense.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:


I would say this is bad regardless of the terniary. I'd rather see this:

 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear wrote:
Yeah, using an if to use the ternary to avoid an if is somewhat iffy.



True. I had actually assumed the if shown from the start was a simplified example of a more complex condition. For example if you had 4 possible paths and 1 of them required a variable to be set one way, the others required the variable to be set another way...

...

fred rosenberger wrote:

Steve Luke wrote:


I would say this is bad regardless of the terniary. I'd rather see this:



But replace the if (someCondition()) with a complicated (3+) conditional statement and I would rather not have it in the ternary.
 
Nageswar Kakolla
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You guys for your opinion.

My 2 cents,

1. first it reduce code from 3 to 5 lines to single line

2. Also, if it was poor programming style, why would java authors would keep it in the first place

3. Whether you are from C background or not, You will learn your programming language features in Java and sometimes unlearn if you are coming from other programming languages background.

I always like to use ternary operator as it simplifies number of lines period.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nageswar Kakolla wrote:I always like to use ternary operator as it simplifies number of lines period.


Number of lines is a poor metric to rely upon. Rather, use whichever makes the most sense and gives the code most clarity.
 
Marshal
Posts: 79943
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nageswar Kakolla wrote:. . .

2. Also, if it was poor programming style, why would java authors would keep it in the first place

I suspect a lot of old features from C++ were retained in Java so as not to frighten off C++ programmers. People are very conservative, so features were kept and we are left with things like

int i, j k[];

not to mention labelled break.

[edit}As for labelled break, I presume you have seen this thread?[/edit]
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could be wrong but the trenary operator is in many languages and I believe it was around before C++. Besides, the more differences I found in Java coming from a C++ background the more I like it On that note... why is it so much easier to visualize references rather than pointers?

Just remember when writing code, you may not come back to edit it for many years. How much of it will you understand at a first glance? Use any tricks/shortcuts sparingly unless reading it makes perfect sense. Also, add a comment!
 
Campbell Ritchie
Marshal
Posts: 79943
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't mean ?: was poor style. I meant int i, j, k[]; was poor style. And I agree that the differences from C++ are mostly improvements.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic