How to Make All Your Code Twice as Complicated

The way to make everything in your codebase twice as complicated is to make data nullable.

foo: str | None

It starts out innocuous but the combination of nullables causes the state space of a program to explode, introducing hard to find bugs and making it hard to maintain over time due to high cyclomatic complexity.

What should you do about it?

By default, data should be non-nullable. Introduce nullables only if you have to (though it’s an indicator the code should be refactored to make more states invariant).

For database schema changes, maybe an optional column needs to be introduced because it can’t be backfilled right away. Great, make it optional, complete the data migration, then migrate the schema to make the column non-nullable.

See also: