On Fri, Dec 8, 2017 at 11:33 AM, Vincent Massol <[email protected]> wrote: > Hi, > >> On 8 Dec 2017, at 11:12, Thomas Mortagne <[email protected]> wrote: >> >> Hi devs, >> >> Most Object#hashCode() are implemented using HashCodeBuilder. >> HashCodeBuilder provide an empty constructor and also allow setting >> "random" numbers. >> >> http://dev.xwiki.org/xwiki/bin/view/Community/JavaCodeStyle#HEquals2FHashCodeandToStringimplementations >> does not really say anything about what do do and when currently so I >> would like to have a clear rule about it. >> >> On my side when I don't plan to use different types of objects in a >> hash key I usually simply use the empty HashCodeBuilder constructor. >> The only point of setting different numbers is when you might end up >> comparing different object types (and even in that case you have the >> #equals() safeguard anyway so it's mostly about performances in >> practice). >> >> So here is a proposal: it's OK to use empty HashCodeBuilder constructor >> except: >> * when overwriting the hachCode of a super class you should use >> different numbers than the super class (since there is big chance to >> have that super class used as a hash key) >> * as a best effect in general if you know that you are going to have >> several different types as hash key, try to make sure they use >> different random numbers, your proc will thank you :) >> >> WDYT ? > > I don’t fully master the topic but I’m trusting you so +0 from me. Only > comment is that it means there’s not a single way of writing our code and > thus is not that easy to check.
The problem is that It's not easy to have a single way to write our code here, if you want one it's going to be "it's OK to use empty constructor of HashCodeBuilder" and the rest will move somewhere like http://platform.xwiki.org/xwiki/bin/view/AdminGuide/Performances#HMemory :) In practice those are more performances related advices because saying something like "all classes must use different numbers" is simply impossible to enforce/check in practice because it's way to hard to know if you are using unique numbers or not. > > Thanks > -Vincent > > >> >> -- >> Thomas Mortagne > -- Thomas Mortagne

