Software Design/Validate data during object construction
Checklist questions:
- The arguments to a constructor or a factory function are validated before an object is returned?
Why
[edit | edit source]Don't allow bad data to silently propagate through (and outside of) the system: be written to a database, sent to another service, which has the potential to destabilize the system and thus making it less mistake tolerant and fault tolerant. Allowing erroneous data to stay in the system for longer also undermines debuggability of the code and traceability of (faulty) behavior in the system.
Why not
[edit | edit source]Data validation consumes CPU resources, while the data validation might be already validated elsewhere if the object is created within the trust boundary for the data. The compromise solution, in this case, may be to use assertions to avoid validating data repetitively in the production environment, but retaining an option to enable them during testing or a debugging session.