Illegal state in a program is always a problem — that's uncontroversial.

How you defend against this happening is much more contested.

Defensive programming, invariants, static analysis, proof assistants — a lot of things have been tried.

Advocates of Strong Types, however, favor this One Weird Trick:

Make Illegal States Unrepresentable

Steam locomotive hanging out of the shattered facade of Gare Montparnasse, 1895
Train wreck at Gare Montparnasse, 1895. An illegal state, represented. (Photograph: Studio Lévy & fils, public domain.)

This website shows what happens as you refine a very basic "struct" from a C-like language, a few tweaks at a time. You whittle down the vast space of illegal states until you reach the point where no illegal state can be represented at all.

We'll show this by algorithmically generating counter-examples — illegal states — for each representation, until we have one that can no longer represent any. The generator is SmallCheck-style bounded-exhaustive enumeration: every value the type can hold, up to a small bound, checked against a single safety oracle. The testing and the structure go hand-in-hand: you don't just trust the structure. But if you have the types and the tests right you no longer need, at a minimum, the same level of defensive programming.