Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 743
kiel814 Avatar asked Oct 17 '13 18:10

kiel814


3 Answers

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.

like image 153
Martijn Courteaux Avatar answered Oct 22 '22 05:10

Martijn Courteaux


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?

like image 26
MrBackend Avatar answered Oct 22 '22 04:10

MrBackend


I would just do this.

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

and it would remove the warning.

like image 2
Neha Agarwal Avatar answered Oct 22 '22 03:10

Neha Agarwal