★ Type Systems Save You from Bugs
— 1 minute read
In Types and Bugs Rafael Ferreira discusses the advantages of type systems that help to discover bugs which unit and integration tests cannot find. He references a very interesting paper in which the author describes how he discovered bugs by porting an algorithm from Python to Haskell. Rafael stats
Most of the bugs fell into one of two categories:
- Assuming that a variable always references a valid value when it can contain a null value.
- Referencing constructs that no longer exist in the source.
NULL is definitely the most annoying concept ever invented in programming. Briefly, it is just a special construct signaling invalid values. NULL is the source of a plethora of bugs, e.g., when ”Dereferencing NULL Pointer, without a Seg Fault”. Today, we have programming languages that express invalid values much cleaner and type safe. See for example Scala’s Option type.
Rafael continues
some method or variable was present but changed, perhaps it was renamed or subsumed, and not all references were updated to reflect the change. Even pervasive unit tests can’t hope to catch these kinds of regressions, as the problem is found on the integration between units of code; the units themselves are just fine. A type system helps when the change affects the signature of the referenced construct, which is often but not always.
You cannot always chose the programming language, because of dependencies. But if you can, use languages that have sophisticated type systems. They safe you from a lot of trouble just by running the compiler.