Everybody can write code, but there are some indicators which tell you whose code is “better”. At least in terms of readability and error-proneness.

Imagine you just joined a development team and you want to extend the functionality of an existing method. Some crazy guy told you that you can get the info you need from a not documented api call. He said something like: “If you call that function it should return a String object. This is what you need.”.

Your part is to figure out what to do depending on the returned value saved in the someString variable.

Which line do you prefer and way more important, why?




You picked the first or think it doesn’t matter? Congratulations! You are the target of this post 🙂

If someString is different from null it doesn’t matter which one you choose because that is the way equals should be is implemented. If the method called in the example above returns null and you try to call equals on that null object it will give you a NullPointerException. To state the obvious: If you use the constant first you won’t get that problem? Why? Because constants are not null and if you call “constant”.equals(null) it will simply return false without any exceptions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.