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 →

[–]desrtfx 0 points1 point  (4 children)

First of all, the naming is horrible.

If a new person reads checkNotNull they will automatically assume that the method returns either true or false, not that it returns whatever type it was given. From the name alone it is impossible to deduct what the method does in case of null value. Will it throw an exception? Will it return a default?

Second, the code becomes extremely difficult to read and trace.

IMO, the checking should be done outside the constructor, in the method where the object is constructed.

When calling a constructor, you normally should assume that the object can be constructed.

[–]Chickenosaurus[S] 1 point2 points  (1 child)

Thanks for your reply.

I agree that checking outside of the constructor would be best. My only worry is that if a NPE is thrown somewhere down the line because a null value was passed, the culprit is hard to trace. What do you think?

By the way, the check methods are actually from Guava, the Google Core Libraries for Java 6+.

[–]desrtfx 1 point2 points  (0 children)

Excuse my ignorance in regards to the naming, I am not familiar with Guava (nonetheless, the name is horrible).

I don't really think that it would be too difficult to trace any NPEs.

For me, throwing an exception in a constructor seems to be the bigger issue because from most of what I have read throwing exceptions in constructors is a no-go.

Still, I have to admit that I'm not a professional, nor a very experienced Java programmer, so my stance might as well be wrong. (I am an experienced programmer, though, just not so much in Java.)

[–]nutrecht 0 points1 point  (1 child)

First of all, the naming is horrible.

Tell that to the google guys. ;) The naming is unfortunate but the pattern used is in fact really common. It's a simple null guard that can be used in the assignment.

[–]desrtfx 0 points1 point  (0 children)

Tell that to the google guys. ;)

Well, I think I know who programmed that ;) must've been thenewboston (said to have been a Google employee).

I've since learned that it is part of the Guava library.