Question

Netbeans gives me The conditional statement is redundant warning for this:

return (this.getId(person) == null) ? false : true;

And if I comply with that, it changes to this:

return (this.getId(person) != null);

Are these two equal? How is the second one conditional?

Was it helpful?

Solution 2

You can unwrap the first form to something like this:

boolean cnd = (this.getId(person) == null);
if (cnd)
  return true;
else
  return false;

This clearly shows that switching on a boolean value just to return the same boolean value is redundant.

The second statement is not conditional but it provides the same result with fewer hops.

OTHER TIPS

Yes , They both are conceptually same.

The second one is not conditional. Its an expression resolves into boolean, So simply you can result of that expression.

Why it is redundant in first case is , It's just like writing

   if(true){
      return true;
    }else{
     return false;
   }

Look at your condition

 this.getId(person) != null;

your this.getId(person) may be null or may not. So that expression resolves as true or false . That's it. Right ? So your IDE is telling that use that expression result.

The expression this.getId(person) == null will return you true or false. Again adding a ternary operator is redundant.

You are returning a boolean value and

return (this.getId(person) != null);

is a boolean conditional statement that will evaluate to true or false. This is why the compiler is telling you that the conditional statement is redundant. It will still compile the other way but its more typing and it's not as clean.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top