Eclipse likes to generate equals
methods (for classes having no super class) with a null
check like this:
if ( null == obj )
return false;
However, I like
if ( obj == null )
return false;
much more, because it is more readable. (It kind of disturbs me all the time.)
Q:
Why does Eclipse generate it with null
coming before obj
, given that the if ( null == obj )
is an obsolete practice stemming from C/C++ as described here?:
(obj == null) vs (null == obj)?
Are there any (runtime) differences between the two? I can only speculate...
Update:
Eclipse Kepler seems to generate if ( obj == null )
, so this applies to former Eclipse versions only.
Class before:
public class Burp
{
private Long id ;
public Burp()
{
// test
}
}
Class after:
public class Burp
{
private Long id ;
public Burp()
{
// test
}
// hashCode() omitted
@Override
public boolean equals( Object obj )
{
if ( this == obj )
return true;
if ( obj == null )
return false;
if ( getClass() != obj.getClass() )
return false;
Burp other = ( Burp ) obj;
if ( this.id == null )
{
if ( other.id != null )
return false;
}
else if ( !this.id.equals( other.id ) )
return false;
return true;
}
}
I think we have some cleaning up to do for our pre-Kepler generated equals
methods.