0

sonar complain Ternary operators should not be nested.
Is there any way possible to remove this complaint since I am beginner in java I would like some help in this issue.

object form = null;
if(objs.getForm() != null)
form = objs.getForm();
String getName = form != null
                     ? referenceObjType + form.getName()
                     : "" + (objs.getType() == null ? "" 
                     : "(" //$NON-NLS-1$
                     + objs.Type().getTypeName() + ")" 
                     + objs.getName());
Maha
  • 15
  • 1
  • 7
  • You can look up the description of sonar rules. For example here: https://jira.sonarsource.com/browse/RSPEC-2583 The statement says that the "else" block of your expression can never be reached. So form can never be null or you call a form. before the given statement. – TomStroemer May 27 '20 at 12:45
  • @TomStroemer so what will be the approach ? can you please provide a sample – Maha May 27 '20 at 12:52
  • if your form can't be null you can just use ```String getName = referenceObjType + form.getName()```. For more information you need to show more code. Where does the form variable come from? Do you access it before the code you've shown? – TomStroemer May 27 '20 at 13:10
  • @TomStroemer yes i do access form variable before this condition . i have edited the code.please find above – Maha May 27 '20 at 13:13

3 Answers3

0

You need to use parenthesis to group the second ternary operator:

String getName = form != null
                 ? referenceObjType + form.getName()
                 : "" + (objs.getType() == null ? "" 
                 : "(" //$NON-NLS-1$
                 + objs.Type().getTypeName() + ")" 
                 + objs.getName());

In java the additive operator (+) has a higher preference than the equality operator (==). see: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

So without the parenthesis the statement

"" + objs.getType() == null

The String concatenation is done first and the result then checked against null. Since an empty String that you append something to cannot be null, this statement will always yield false and one part of your ternary operator will never be reachable.

OH GOD SPIDERS
  • 3,091
  • 2
  • 13
  • 16
  • after using the parenthesis i am getting Ternary operators should not be nested (squid:S3358) complain @OH GOD SPIDERS – Maha May 27 '20 at 12:58
  • That is probably because as you can probably see from your own code nested ternary operators aren't really great for producing readable code. You should think about just rewriting the code completly and using a StringBuilder and normal if-else blocks. But if you want to keep using nested ternary operators see: [Can Ternary operators should not be nested (squid:S3358) be configured](https://stackoverflow.com/questions/53369046/can-ternary-operators-should-not-be-nested-squids3358-be-configured) – OH GOD SPIDERS May 27 '20 at 13:14
0

If you are a beginner with Java I would refrain from using ternary operators so you could really understand what each statement is doing.

The issue: I believe the else statements (the ones starting with ":") are not reachable because form will never be null. Also you are appending a string prior to checking if it is null. This will always return that it is not null. You also need to group your second ternary operator with a parentheses like so:

   String getName = form != null
             ? referenceObjType + form.getName()
             : "" + (objs.getType() == null ? "" 
             : "(" //$NON-NLS-1$
             + objs.Type().getTypeName() + ")" 
             + objs.getName());

It is hard to give you a complete answer since I do not know your original problem and cannot see other code but I hope this helps

muzykfs
  • 62
  • 3
0

What about something like this? Nested ternary operators are quite bad practice.

    String getName= "";
    if (form != null) {
        getName = referenceObjType + form.getName();
    } else {
        getName = objs.getType() == null ? "" : String.format("(%s)%s",objs.Type().getTypeName(), objs.getName());
    }
kosta.dani
  • 447
  • 2
  • 11