4 ms·
That article raised a frown with me. It presents some Java code, then shows the Ruby version, which ends up being much more concise. But the Java code is needle
by twic 12y ago
That article raised a frown with me. It presents some Java code, then shows the Ruby version, which ends up being much more concise. But the Java code is needlessly verbose. Here's how i'd arrange it:
@Override
public boolean equals(Object object) {
if (!(object instanceof Gallery)) return false;
Gallery other = (Gallery) object;
return (this.gid != null || other.gid == null) && (this.gid == null || this.gid.equals(other.gid));
}
Apart from the cast, which you need because Java has static types and Ruby doesn't, it's the same length. It's basically the same code, token for token.
Actually, the logical structure of the final line is a bit obscure to me; i'd write it:
return this.gid != null ? this.gid.equals(other.gid) : other.gid == null;
Or, in Java 7 or later:
return Objects.equals(this.gid, other.gid);