aspose file tools*
The moose likes Java in General and the fly likes ? ternary operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "? ternary operator" Watch "? ternary operator" New topic
Author

? ternary operator

Nageswar Kakolla
Ranch Hand

Joined: Jan 16, 2006
Posts: 71
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

Joined: Jan 10, 2002
Posts: 61661
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19003
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
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.

SCJA
~Currently preparing for SCJP6
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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


Steve
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
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

Joined: Jan 10, 2002
Posts: 61661
    
  67

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

Joined: Oct 02, 2003
Posts: 11480
    
  16

Steve Luke wrote:


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

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Jan 16, 2006
Posts: 71
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

Joined: Jan 10, 2002
Posts: 61661
    
  67

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

Joined: Oct 13, 2005
Posts: 39884
    
  28
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

Joined: Nov 07, 2008
Posts: 488
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

Joined: Oct 13, 2005
Posts: 39884
    
  28
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.
 
 
subject: ? ternary operator