• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

? ternary operator

 
Nageswar Kakolla
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • 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

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic