lrod408
lrod408

Reputation: 135

Redundant If message

I have my Fraction program running smooth but the NetBeans IDE is telling me the following if is redundant:

public boolean equals(Object other)
{
  Fraction bool = (Fraction) other;

  if(this.numerator == bool.numerator && this.denominator == bool.denominator)
  {
   return true;
  }
  else return false;       
} 

The above code compiles / runs perfectly and passes all the test cases but the redundant flag by NetBeans is really bothering me. I add reduceToLowestTerms() to my code and the flag goes away but I already have reduceToLowestTerms() in my constructor. This is what the non-redundant code (according to NetBeans ) looks like:

public boolean equals(Object other)
{
        Fraction bool = (Fraction) other;

        if(this.numerator == bool.numerator && this.denominator == bool.denominator)
        {
         bool.reduceToLowestTerms();
         this.reduceToLowestTerms();
         return true;
        }
         else return false;       
    } 

Any suggestions would be greatly appreciated

Upvotes: 1

Views: 169

Answers (1)

rgettman
rgettman

Reputation: 178293

This appears similar to a warning my IDE gives on this statement:

'if' statement can be simplified

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

can be simplified to

return foo();

It's just overly complicated and verbose code. Your simplification would be:

return this.numerator == bool.numerator && this.denominator == bool.denominator;

But as you've noticed, your code is already correct. It's not necessary to make this change, but it will make the code more concise and more simplified.

The reason that adding calls to another method (reduceToLowestTerms()) removes this "flag" is that the code can no longer be simplified in this way to a single return statement.

Upvotes: 8

Related Questions