4

Eclipse likes to generate equals methods (for classes having no super class) with a nullcheck 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.

Community
  • 1
  • 1
Kawu
  • 13,647
  • 34
  • 123
  • 195
  • 1
    `if ( obj = null )` yields an error in Eclipse and any correct Java compiler. – Kawu May 14 '14 at 14:20
  • 1
    You may end up with the answer "It had to be one way round and what you consider more readable isn't necessarily what everyone considers more readable" – Richard Tingle May 14 '14 at 14:23

4 Answers4

4

Some people write null == obj instead obj == null, because there is no risk to type = instead of ==, but it doesn't change how your code works. Keep in mind that in Java it's only possible to write obj = null (without an error) when obj is Boolean. This way of writing code comes from other programming languages like C where it's completely valid to write obj = null by mistake.

Jakub H
  • 2,130
  • 9
  • 16
  • If Eclipse generates this kind of code, why should "typing" be an argument? – Kawu May 14 '14 at 14:08
  • Guess whoever wrote the template typed it that way? – awksp May 14 '14 at 14:08
  • Good point. People read from left to right, and i can devine what will follow after `if (null == ` much more than what will follow after `if (obj == `. – Grim May 14 '14 at 14:11
  • @assylias you're correct it can't be called convention, I've removed that. Some people just think it's a good practice. – Jakub H May 14 '14 at 14:14
  • 2
    `if ( obj = null )` yields an error in Eclipse and any correct Java compiler. – Kawu May 14 '14 at 14:21
  • For example if your obj is Boolean there is no error! Btw why 2 down votes? – Jakub H May 14 '14 at 14:31
  • 1
    The argument to type `if ( obj = null )` is no danger because a Java compiler (Eclipse) will catch it, see my comment above yours. – Kawu May 14 '14 at 14:36
  • Type this `Boolean a = true; if(a = null) {}` in your compiler and you'll see that's possible. – Jakub H May 14 '14 at 14:41
  • 1
    Yes, but do you generate `equals` methods for the `Boolean` class? – Kawu May 14 '14 at 14:44
  • 1
    I'm just saying it's possible in Java in general and why people use it. In case of equals method it's not possible to make such mistake, so null == obj was generated, because author of this template probably writes his code that way every time in every class and method. This style of writing besides readability doesn't have any other disadvantages, but I prefer to write obj == null. – Jakub H May 14 '14 at 14:49
3

You can modify the template for generating equals method in Eclipse, refer the following link

As far as Runtime differences between the two are concerned I think both the expressions should be the same as the compiler would optimize both the null check expressions in a similar way (i.e same bytecode will be generated). Its more about what conventions people like to use , and it varies from person to person.

Kakarot
  • 4,252
  • 2
  • 16
  • 18
2

There is no difference and the convention in Java (if we assume that the JDK is a reference) is to use if (obj == null). See for example the code of Objects#requireNonNull:

public static <T> T requireNonNull(T obj) {
    if (obj == null)
        throw new NullPointerException();
    return obj;
}

For what it's worth, Netbeans auto-generates equals with if (obj == null) return false;.

Whichever you use won't make a difference at runtime and will generate the same bytecode.

assylias
  • 321,522
  • 82
  • 660
  • 783
1

also some people like Master Yoda style where you compare constant and then varible

if (5 == i) -- Yoda style

Alex G
  • 41
  • 3