Shivam Malhotra
Shivam Malhotra

Reputation: 309

How do I simplify an IF statement that returns true or false?

public bool CheckStuck(Paddle PaddleA)
{
    if (PaddleA.Bounds.IntersectsWith(this.Bounds))
        return true;
    else
        return false;
}

I feel like the above code, within the procedure, is a bit redundant and was wondering if there was a way to shorten it into one expression.

At the moment if the statement is true, it returns true and the same for false.

Is there a way to shorten it?

Upvotes: 8

Views: 33301

Answers (2)

Sebastian Redl
Sebastian Redl

Reputation: 72044

You can always shorten an if-else of the form

if (condition)
  return true;
else
  return false;

to

return condition;

Upvotes: 12

Bill the Lizard
Bill the Lizard

Reputation: 405955

public bool CheckStuck(Paddle PaddleA)
{
    return PaddleA.Bounds.IntersectsWith(this.Bounds);
}

The condition after return evaluates to either True or False, so there's no need for the if/else.

Upvotes: 22

Related Questions