DEV Community

Discussion on: Unhealthy Code: Null Checks Everywhere!

Collapse
 
cfv1984 profile image
CFV • Edited

Optional types are where it's at, and there's some prebuilt ones that are more or less free to use in terms of complexity.

Rearchitecting your entire application to deny the fact that nulls exist as a valid value strikes me as basically the nuclear option when compared to dealing with them head on.

Collapse
 
kopseng profile image
Carl-Erik Kopseng

It is not necessarily about nulls, but what to do in the absence of a value. You can often find multiple types of these null classes to cover different cases. And it does mean you have to rearchitect anything. For instance, when designing a Card component in a React application, that was dealt a User object, we assumed it would always exist. Suddenly, it was potentially optional, but that would imply changing user: User to user: User | undefined through multiple levels. Simply creating a NullUser that was returned at the top level selector function allowed us to make 0 (zero) changes further down.