Parents, teenagers, and unit tests

A perspective of unit tests

Parents, teenagers, and unit tests

There’s a theory of stability. It applies to entities. We can observe its effect on human behaviour. It explains why parents tend to be more stable individuals than teenagers. And can explain why unit tests make code more stable.

The theory says stability increases with dependants. Let’s explore this by juxtaposing parents and teenagers. A typical teenager doesn’t have dependants. They can afford to change their behaviour. They can change their sleeping patterns. They change their discipline, perhaps to play more computer games and study less. Because their changes, for the most part, only affect them. Parents are different.

Parents have dependants, their children. If they’re conscientious, this makes them stable. They can’t change their sleeping patterns, because children may miss out on important activities. They have to stay disciplined, be it to keep their job as a breadwinner or to help their children at home. Their behaviour remains consistent because changes to it may have a bad effect on their dependants. You may now think, 'That's commendable. But how is this relevant to unit tests?’

Think of unit tests as the expectations of what your code’s supposed to do personified as dependants. Writing them gives your code dependants. In theory, a unit of code's stability should go up when you make tests depend on it. Dependants can also provide feedback to their dependency about how well they're regulating their behaviour. A child can inform their parent if things are wrong. Unit tests also do this. Not to their units but to software engineers. They tell you when the changes to their dependency, the unit, is wrong.

That's it! A strange perspective of unit tests. They're dependants you give to units of code to increase stability.