Basically as the title says. Say we have an API endpoint that takes some object as a parameter and passes that object into a service method. That service method then relies on a mapper class to map the object to another type. This leaves three places to check if the object is null:

  1. Beginning of the API function
  2. Beginning of the service function
  3. Beginning of the mapper function

It seems overkill to perform a null check three times, but there is no guarantee that the service function will only be used by that one API function and no guarantee that the mapper function will be used only by the service function. I was curious what others considered to be best practice in this situation. Thanks!

  • 2
    Well the best way to use a non-nullable variable. If it's just a mapper class, why not just map the null value to the target? Aug 21, 2020 at 18:57
  • @RobertHarvey Do you mean just returning null? Or returning an "empty" object, i.e. not null but has no properties set?
    – Odd822
    Aug 21, 2020 at 19:31
  • Which way is most faithful to the properties found in the original object? Aug 21, 2020 at 19:34
  • Also, what are your design requirements? If your requirements say that one of the responsibilities of the mapper is turning nulls into empty objects, then that's what you do. If your requirements say that the mapper merely passes through whatever it is mapping, then that's what you do, nulls and all. Aug 21, 2020 at 19:38

1 Answer 1


You should check that a parameter is not null whenever* you write a method that cannot process with a null value. If that means it gets checked 3 times, then so be it. Some languages have a specific exception for this case (e.g., ArgumentNullException in C#), which is an appropriate course of action when a null parameter is encountered.

If your method can process as normal with the null value, there's probably no need to check it. As Robert Harvey mentioned in the comments, if you can add a mapping from a null value to the correct target, go with that and there is no need to check for a null parameter. If you can't add that mapping (e.g., a null value doesn't make physical sense), then check the parameter for null first.

  • Typically this only applies to public methods. While you know that you've checked the value for null in the API function, the service function won't necessarily always get called from the API function, as you said. You control the usage of private methods much more tightly, so there is not as big a concern that they get used differently than originally written.
  • You should also check for null before dereferencing a variable that might contain null. Aug 21, 2020 at 19:35
  • ...unless throwing a null pointer exception is your desired behavior. Aug 24, 2020 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.