Java: if statement can be simplified (box contains point)

kiel814 picture kiel814 · Oct 17, 2013 · Viewed 10.2k times · Source

I have the following statement to check if a Vector2D is within a box, and IntelliJ gives me a warning: "if statement can be simplified".

if(point.x < minX || point.x > maxX || point.y < minY || point.y > maxY)
    return false;

How can I simplify this?

Answer

Martijn Courteaux picture Martijn Courteaux · Oct 17, 2013

I don't see any possible simplification. I would just ignore the warning.


Update: However, if you method look like this:

if(point.x < minX || point.x > maxX || point.y < minY || point.y > maxY)
    return false;
return true;

You might change it to this:

return !(point.x < minX || point.x > maxX || point.y < minY || point.y > maxY);

Or even:

return point.x >= minX && point.x <= maxX && point.y >= minY && point.y <= maxY;

I don't know if this is "simplified" for humans.