Skip to content

Conversation

@ttomczak3
Copy link

@ttomczak3 ttomczak3 commented Apr 1, 2023

To make the code more concise and efficient, we can simplify the conditional expressions and combine multiple conditions into a single return statement using short-circuit evaluation.

@CLAassistant
Copy link

CLAassistant commented Apr 1, 2023

CLA assistant check
All committers have signed the CLA.

Copy link

@gabrielforster gabrielforster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

damn it

@RickHPotter
Copy link

Best pull request so far. Appreciate the effort.

@hungtruong
Copy link

LGTM

@Quinn-Barber
Copy link

genius

@preland
Copy link

preland commented Apr 1, 2023

Impossible; an actual PR?

On this repo?

@0o120
Copy link

0o120 commented Apr 1, 2023

Groundbreaking. This could be the missing link.

@Claaas
Copy link

Claaas commented Apr 1, 2023

LGTM

1 similar comment
@Conradyen
Copy link

LGTM

@peiwenxu
Copy link

peiwenxu commented Apr 2, 2023

better-algorithm

@hexaquarks
Copy link

Groundbreaking

Copy link

@jaidTw jaidTw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}

return true;
return a.source == b.source;
Copy link

@shailrshah shailrshah Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can reduce the verbosity further.

if (a == null && b == null) {
    return true;
}

return a != null && b != null &&
    a.accuracy == b.accuracy &&
    Math.abs(a.latitude - b.latitude) <= COORDS_EQUALITY_THRESHOLD &&
    Math.abs(a.longitude - b.longitude) <= COORDS_EQUALITY_THRESHOLD &&
    Double.compare(a.radius, b.radius) == 0 && 
    a.source == b.source;

I feel like this is more readable because now we exactly know all the conditions needed for non-null values of a and b to be equal in one concise statement.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that just the last if/return should be reduced, because its the last one. I dont know. I think that the others are kinda "ok" to be like that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, it's definitely more maintainable.

@ttomczak3 ttomczak3 changed the title optimized return optimized approxEquals in GeoObject.java Apr 5, 2023
@davidawad
Copy link

this would be the least lines and most maintainable.

return a!=null&&b!=null&&a.accuracy==b.accuracy&&Math.abs(a.latitude-b.latitude)<=COORDS_EQUALITY_THRESHOLD&&Math.abs(a.longitude-b.longitude)<=COORDS_EQUALITY_THRESHOLD&&a.radius==b.radius&&a.source==b.source;

@ttomczak3
Copy link
Author

ttomczak3 commented Apr 5, 2023

this would be the least lines and most maintainable.

return a!=null&&b!=null&&a.accuracy==b.accuracy&&Math.abs(a.latitude-b.latitude)<=COORDS_EQUALITY_THRESHOLD&&Math.abs(a.longitude-b.longitude)<=COORDS_EQUALITY_THRESHOLD&&a.radius==b.radius&&a.source==b.source;

I noticed that the Double.compare() method was removed. Can you explain the reason for this? It seems to be a more accurate way to compare two double values.

@shailrshah
Copy link

this would be the least lines and most maintainable.

return a!=null&&b!=null&&a.accuracy==b.accuracy&&Math.abs(a.latitude-b.latitude)<=COORDS_EQUALITY_THRESHOLD&&Math.abs(a.longitude-b.longitude)<=COORDS_EQUALITY_THRESHOLD&&a.radius==b.radius&&a.source==b.source;

I noticed that the Double.compare() method was removed. Can you explain the reason for this? It seems to be a more accurate way to compare two double values.

I would stick with the compare() method. Double values can have rounding inaccuracies due to their representation in the computer's limited memory space of (usually) 64 bits.

https://www.baeldung.com/java-comparing-doubles

Copy link

@shailrshah shailrshah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the quick turnaround!

@ttomczak3
Copy link
Author

this would be the least lines and most maintainable.

return a!=null&&b!=null&&a.accuracy==b.accuracy&&Math.abs(a.latitude-b.latitude)<=COORDS_EQUALITY_THRESHOLD&&Math.abs(a.longitude-b.longitude)<=COORDS_EQUALITY_THRESHOLD&&a.radius==b.radius&&a.source==b.source;

I noticed that the Double.compare() method was removed. Can you explain the reason for this? It seems to be a more accurate way to compare two double values.

I would stick with the compare() method. Double values can have rounding inaccuracies due to their representation in the computer's limited memory space of (usually) 64 bits.

https://www.baeldung.com/java-comparing-doubles

Exactly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.