-1

I've seen the ternary operator can be used in if else situations but neither of them included a else if(if-else ladder).So,is there a way to use ternary operator in that situation. I'm new to the java. So doesn't mind if it's silly question.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
R. S. D
  • 33
  • 4
  • Possible duplicate of [Java: Ternary with no return. (For method calling)](https://stackoverflow.com/questions/9450864/java-ternary-with-no-return-for-method-calling) – Andra Oct 25 '19 at 17:16
  • 2
    You can chain ternary operators together if that's what you're asking. For example: `String foo = bar == 0 ? "zero" : (bar == 1 ? "one" : "something else");` – DunderMethod Oct 25 '19 at 17:21
  • 1
    I would be weary of nested ternary operators as they can be difficult to read. I would suggest to separate the internal ternary to its own method. – Omar Silva Oct 25 '19 at 17:24
  • If you are nesting ternary statements, then you might as well just use an if/else if block.... much easier to read – RobOhRob Oct 25 '19 at 17:29
  • @RobOhRob ugly to read but easy to write – Ori Marko Oct 25 '19 at 17:33
  • 1
    @user7294900 not easier to write than an if/else block.... – RobOhRob Oct 25 '19 at 17:33
  • @RobOhRob one liner can be tempting – Ori Marko Oct 25 '19 at 17:35

4 Answers4

3

Yes, but it's consider a code smell according to sonar

While the ternary operator is pleasingly compact, its use can make code more difficult to read. It should therefore be avoided in favor of the more verbose if/else structure

For example continuing checking a after :

 a==b?b:a==c?c:d

Should be according to sonar

if (a==b) {
else if (a==c) {
} else { 
}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • 1
    Just because someone with their own personal preferences managed to get this rule pushed into sonar as a "code smell" doesn't mean there is anything wrong with the code. Just format it clearly. – john16384 May 18 '20 at 11:50
0

Lets take an example for what you are suggesting:

Normal code:

int greatestNumber(int a, int b, int c)
{
    if(a > b && a > c)
    {
     return a;
    }
    else if(b > a)
    {
     if(b > c)
     {
      return b;
     }
     else
     {
      return c;
     }
    }
 return 0;
}

Same code but with Ternary operator:

int greatestNumber(int a, int b, int c)
{
    return (a > b && a > c)? a: (b > a? (b > c? b: c): 0);
}

As you can see it is possible but not recommended as it creates code that is difficult to read

0

Either cascading if-else or nested ternary operations can be considered. Lots of folks prefer cascading if-else statements. I've found nested ternary operations to be acceptable, so long as the expressions are simple and adequate formatting is used.

What will matter is how readable is the resulting code. For nested ternary operations, formatting and properly placed parenthesis are important.

Here are two examples:

public int selectorI(boolean c1, int v1, boolean c2, int v2, boolean c3, int v3, int vDef) {
    if ( c1 ) {
        return v1;
    } else if ( c2 ) {
        return v2;
    } else if ( c3 ) {
        return v3;
    } else {
        return vDef;
    }
}

public int selectorT(boolean c1, int v1, boolean c2, int v2, boolean c3, int v3, int vDef) {
    return ( c1 ? v1 :
           ( c2 ? v2 :
           ( c3 ? v3 : vDef ) ) );
}
Thomas Bitonti
  • 1,179
  • 7
  • 14
  • 2
    I'd say that nested ternary operations are not an acceptable style at least not in any code review I've ever attended. Just because you can do it doesn't make it an acceptable style. Style has to do with how the code looks and its readability and grokness. Readable and style are not necessarily two different things. – Richard Chambers Oct 25 '19 at 17:37
  • It's perfectly readable, if correctly formatted, but most IDE's mess that up and formatting by hand seems to be becoming a lost art. – john16384 May 18 '20 at 11:48
0

Proper formatting styles:

Sample 1:

return c1 ? v1
     : c2 ? v2
     : c3 ? v3
     : v4;

Sample 2:

return c1 ? v1 :
       c2 ? v2 :
       c3 ? v3 :
       v4;  // or on previous line

One reason to use a ternary expression is to only assign a variable once, where you would otherwise be forced to extract a method (with multiple returns).

String type = c instanceof String ? "Text"
            : c instanceof Number ? "Numeric"
            : "Something else";

It's very close to the switch expressions we're waiting for but available now.

john16384
  • 7,800
  • 2
  • 30
  • 44