Atom Feed SITE FEED   ADD TO GOOGLE READER

Be conservative in what you accept and conservative in what you send

It's tempting to write overly-friendly APIs:
  public void setDeliveries(List<Delivery> deliveries) {
if (deliveries == null) {
deliveries = Collections.emptyList();
}

this.deliveries = Collections.unmodifiableList(
new ArrayList<Delivery>(deliveries));
}

Please don't do this:

It masks bugs. The caller might be passing the wrong deliveries object, such as orderDeliveries when they intended this.orderDeliveries.

It makes your code difficult to maintain. Assume that this method has several callers and you'd like to override it. Now the subclass needs to maintain this exact same behaviour. You'll face similar issues if you introduce an interface or do other refactorings.

Instead, constrain your inputs as tightly as possible:
  public void setDeliveries(List<Delivery> deliveries) {
if (deliveries == null) {
throw new NullPointerException("deliveries");
}

this.deliveries = Collections.unmodifiableList(
new ArrayList<Delivery>(deliveries));
}
This code properly explodes on bad input. Code like this is easier to maintain since there are fewer cases to support.

Note that we would have the same behaviour without the explicit null check because new ArrayList<deliveries> throws as well. But I'm coding as much for the reader as for the compiler so I like to be as intentional as possible.

Finally, consider writing or adopting a utility class to encourage writing concise preconditions.
Instead of a NullPointerException, I would throw an IllegalArgumentException on a null argument. This makes it clear that the callee checked the argument as opposed to just dereferencing a null object reference.
I agree with throwing a NullPointerException. It's exactly what it is! I have had the same discussions at the office where others want to throw IllegalArgumentException but I never understood why. It's a null, it isn't suppose to accept a null, it's a null pointer (well, reference). Joshua Bloch had mentioned the issue of null pointer exceptions in Effective Java, years ago.

Another nice thing about a NullPointerException is that it explicitly states what the problem is. If we throw an IAE then we have to place a longer message to describe what the problem is. So, you'll end up writing a silly "deleveries cannot be null". It is just redundant.

I would also state that making the parameter final wouldn't allow you to alter the variable passed in, which I usually find bad form because you might have a strange side effect that you didn't expect.

As a compromise, I would just make sure that the method is documented that if a null is passed the parameter is considered an empty list.
NullPointerException: something was null that wasn't supposed to be. Who knows if it's the caller's fault?
IllegalArgumentException: the caller screwed up, but you'll have to read the code to find out how

It really doesn't matter which type you throw, since nobody should ever be catching these exceptions.
I agree that it doesn't make that much of a difference normally. I still prefer IllegalArgumentException for API methods - they occur at the boundary between components. In that case, it does make a difference in that a NPE would be filed as a bug against the callee's component while an IAE would be filed as a bug against the caller's component. Unless the people filing bugs would first look at the source code, but who has the time to do that?

I don't know about you, but I try to avoid having to look at other people's bugs. ;-)