Search code examples
javasonarqube

Conditionally executed blocks should be reachable


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());

Solution

  • 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());
        }