Skip to content

Handling Null Input

waratah edited this page May 13, 2019 · 1 revision

The way null inputs is dependent on the programmer handling it. Some like nulls being passed straight through and others nulls are almost banned from their code. This is purely a programming choice.

Since Fluent-Mapper is opinionated the default action is the most restrictive of throwing an exception. The choice however is yours.

Null gives exception

Default - will terminate with an argument exception

If you expect data to be present, ie input must have something then the simplest approach is simply to crash and handle it. This avoids errors due to nulls later on or multiple null checks in your code.

Produces ArgumentException (Not a NullException because we are checking the argument being sent in)

Null returns a null result

.WithNullSource().ReturnNull() - Returns null when null given

This is an obvious pattern. Handling null returns can add a lot of code weight. You will be able to tell whether the input data was present or missing.

Null returns a default object

.WithNullSource().ReturnEmptyObject() - Returns an empty object when null given.

This is a good pattern. Handling null in code adds a lot of weight, an empty object often requires little change. The down side is that you will not be able to tell whether the input data was present or missing.