Keetz
Keetz

Reputation: 7

Java sonar violation on "Convert this usage of the ternary operator to an "if"/"else" structure"

stageIndexingCode = StringUtils.isEmpty(indexOperation) 
                  ? stageIndexingCode 
                  : indexOperation;

How can we rewrite this into if else structure? without creating any warnings.

Note: stageIndexingCode is of type String

Upvotes: 0

Views: 2396

Answers (1)

jhamon
jhamon

Reputation: 3691

You should make some research about what is a ternary operator.

if (StringUtils.isEmpty(indexOperation)) {
    stageIndexingCode = stageIndexingCode;
} else {
    stageIndexingCode =indexOperation;
}

shorter form:

if (!StringUtils.isEmpty(indexOperation)) {
    stageIndexingCode =indexOperation;
}

Upvotes: 1

Related Questions