Community
Participate
Working Groups
For consistancy and future developers not inheriting this code due to copy paste, all equals methods that do not try to aggressively exit should be updated. bad pattern: boolean equals = true; equals &= Util.equals(defined, someObject.defined); equals &= Util.equals(description, someObject.description); return equals; changed to: if (!Util.equals(defined, castedObject.defined)) return false; if (!Util.equals(description, castedObject.description)) return false; return true;
fixed HEAD
I've just gone through and tweaked these a bit. The code has the following pattern: if (!boolean) return false; return true; Which I've changed to: return boolean;
Hi guys. You might also consider the following replacement as well, which guarantees at least as good performance characteristics as the previously suggested replacement: return Util.equals(defined, castedObject.defined) && Util.equals(description, castedObject.description) && Util.equals(id, castedObject.id) && Util.equals(name, castedObject.name) && Util.equals(parentId, castedObject.parentId); Thanks to short-circuit evaluation (which is guaranteed by the language spec) this expression will always aggressively exit on the first false term. And it saves a bunch of ifs, nots, and returns in the code. The pattern I originally used wasn't intentionally designed to look n00b. The problem was the bitwise-and assignment operator &=. Java does not have a logical-and assignment operator, which if it did might look like this: &&=). This was my oversight. The compiler was happily and without warning treating the boolean r-value as an integer, and as a result was unable to optimize further.
Another small point for performance is that equals() would do nicely to check if the instance is identical before bothering to compare fields for equality. The general pattern for class X with fields f1,f2,f3 then becomes: X.equals(Object object) { if (object == this) return true; if (!(object instanceof X)) return false; X castedObject = (X) object; return Util.equals(f1, castedObject.f1) && Util.equals(f2, castedObject.f2) && Util.equals(f3, castedObject.f3); }
Regarding comment #4: I had adopted this pattern in the "org.eclipse.core.commands" package. It may not be adopted universally through all of the workbench.
verified by checking a few classes I fixed on Version: 3.1.0 Build id: I20050509-2010