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?
Author by
kiel814
Updated on June 04, 2022Comments
-
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 over 10 yearsThis is not equivalent to what OP is asking.
-
kiel814 over 10 yearsThere was no lightbulb in my version, but there was a clickable tool tip. Thanks!
-
kiel814 over 10 yearsThis 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.