Your code is bad practice because it allocates a new instance. Try to use Collection.emptyList()
instead.
Drawback: If the consumer of your code wants to change the list, they need to make a copy of the list. But that is good practice since many frameworks return immutable lists and as a consumer, you can never be sure unless the API actually says what you are allowed to do with the result.
Returning null
is bad because that exports an implementation detail of your code to the consumer. Or to put it another way around: If you change your implementation and you suddenly find you want to return null
, you probably need to add checks in all the places where the API is used. So it hampers your ability to evolve your code and your API.
For other (non-collection) types, you should look at the "optional" pattern as described here: https://stackoverflow.com/a/16218718/34088
This pattern allows you to tell consumers of your API that some method might return "nothing" without ever returning null
. That way, consumers know what to expect.