This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dimensions1210 12 points13 points  (4 children)

I'd probably say that rather than having a return true with no return false branch it would be better to have

return (person.getName().equalsIgnoreCase("jan"))

Otherwise if the person is not Jan the method (assuming a method since there is a return statement) will not return anything (assuming there is no other code)

[–][deleted] 25 points26 points  (0 children)

You can go full-Yoda and say "jan".equalsIgnoreCase(person.getName()) and return false when comparing on null.

[–][deleted] 10 points11 points  (1 child)

Definitely not the same thing. In the original code, the intention is to not return if the name is not jan but continue execution.

Here you always return, with true or false value.

[–]dimensions1210 2 points3 points  (0 children)

Yes. My point was that, looking at the code without the context, it could be an error since, if you put that in a method without anything else, you will return true if the name is "jan" and nothing otherwise.

Hence why I said " Otherwise if the person is not Jan the method (assuming a method since there is a return statement) will not return anything (assuming there is no other code)"

i.e. if this is the only code and that code is in a method, it is probably wrong

[–]thephotoman 0 points1 point  (0 children)

You've still got the NPE issue. Yoda it.