Originally Posted by orchid
|
|
It is not terribly readable. Why woulc anyone use it.
|
Most people consider ternary operator to be difficult to read, we can expand thus wherever found:
|
Code:
|
( variable1.equals(variable2)? 5: 4 ); |
to
|
Code:
|
if(variable1.equals(variable2))return 5;
else return 4; |
I use ternary operator once in maybe 500 lines of code, that's when I am in a mood to use it.
Especially challenging, if you want, is to code:
|
Code:
|
if(variable1.equals(variable2))
{
if(variable2.equals(variable3))
{
System.out.println("proceed");
}
else
{
System.out.println("not available");
}
}
else
{
processData();
} |
In ternary operators.
Looks convoluted in ternary operators, and is especially tangled to sort out when one does not have a great deal of code already written. What evenutally evolves is something called Trees, which remarkably can be written: ( ( ( ( ) ( ) ) ( ) )( ) )