[ajug-members] Ternary Operator - simplification or complication?

Jack Coleman jackjc at bellsouth.net
Thu Jun 1 14:14:45 EDT 2006


I find conditional operator "?" to be quite useful. I use it all the time.

The biggest problem I've seen is how some programmers use it when they 
don't need to.

Say you want to assign a value to a boolean "b" if  "n" is greater than 1.

You could do this (if you are new to programming):

if( n > 1 ) {
    b = true;
} else {
    b = false;
}

Or you could use the conditional operator (I see this all the time)

b = n > 1 ? true : false;

Better to just do this:

b = n > 1;

I have found the conditional operator very helpful in Jasper reports where you have to use expressions in the report fields.




Jonathan Komorek wrote:
>
> I just opened up a can of worms within our development community by 
> stating that in certain cases, I find that the Ternary Operator 
> simplifies code. I've been given the impression that I'm the last Java 
> developer alive that believes the ternary operator should ever be 
> used. I'm interested in hearing the opinions of users on this group.
>
> Has the ternary operator helped you? Caused you great grief? Should it 
> even be in the language?
>
>
> *Jonathan Komorek*
> /Development Security Officer/
> Benefitfocus.com, Inc.
> (843) 849-7476 ext. 254 (Main)
> (843) 849-9485 (Fax)
> E-mail: jonathan.komorek at benefitfocus.com
>




More information about the ajug-members mailing list