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

11,854

Solution 1

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.

Solution 2

Whenever IntelliJ warns of a possible simplification, it often offers to perform the simplification (by clicking the yellow light bulb). What happens if you do that?

Share:
11,854
kiel814
Author by

kiel814

Updated on June 04, 2022

Comments

  • kiel814
    kiel814 almost 2 years

    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?

  • Smit
    Smit over 10 years
    This is not equivalent to what OP is asking.
  • kiel814
    kiel814 over 10 years
    There was no lightbulb in my version, but there was a clickable tool tip. Thanks!
  • kiel814
    kiel814 over 10 years
    This was indeed the case. I was lookig for a simplification inside the if clause. That's why I couldn't find it. I went with your last suggestion; it made more sense to me.