Validations and Object Creation

--

When it’s possible to instantiate an invalid object, it will happen when you don’t expect it.

An object that accepts invalid states lacks type safety.

These objects are a shaky foundation for building complex systems. You can’t treat them as values as they are mutable. You can’t rely on their state because it can be invalid. You can’t treat them as canonical sources of information as their shape depends on the query logic, which can be dynamic.

Validations can help enforce constraints across different contexts. Instantiation in a valid state enforces stability constraints from the start. Validations reduce the risk that your system will crash due to an invalid state but are no guarantee.

Object creation and validation are two different concerns. One deals with the initial state, and the other with the transient state.

--

--

Rafael A. George Duval
Rafael A. George Duval

Written by Rafael A. George Duval

✍🏼 Building a Solo Digital Media Company 🧪 Snippets of Text [https://snippetsoftext.substack.com/subscribe]

No responses yet