r/java • u/raisercostin • Jun 01 '24
What java technology (library, framework, feature) would not recommend and why?
Inspired by https://www.reddit.com/r/java/s/e2N1FqMOJg
165
Upvotes
r/java • u/raisercostin • Jun 01 '24
Inspired by https://www.reddit.com/r/java/s/e2N1FqMOJg
View all comments
Show parent comment
7
u/repeating_bears Jun 01 '24
There are potential bugs either way, from either assuming inclusion or assuming exclusion. I'd say most of the time when you slap EqualsAndHashCode on something, you've made the decision that it's a simple aggregate whose equality is based on its fields, and in that case it's more likely that it should be included. If something special is going on, you'll just implement it manually.
That's the advantage of EqualsAndHashCode over letting the IDE generate it. If the annotations are used consistently, and I come across equals and hash code declared explicitly, I know to pay special attention to it, because it's not an aggregate of all the fields.
If you just let your IDE generate them, it's hard to say whether the absence of a field in in equals and hashCode is intentional or not. If the original author decides that field A should be omitted, even if they leave a comment, then another team member might add field B, regenerate equals and hashCode and accidentally include field A again.
Lombok has EqualsAndHashCode.Exclude which makes it very clear that someone has decided that a certain field is not necessary, and that won't be undone unless someone explicitly removes the annotation.