The Long-lasting Dilemma
Roughly speaking, there are three main drawbacks of using static types:
- Increased verbosity
- Reduced flexibility, eliminating some of the benefits of dynamically-typed languages.
- Added complexity in tooling and increased cost for training/learning.
Equally, there are three main benefits of static types:
- Better tooling support (better autocomplete, visible types)
- Reducing the number of redundant tests checking for type errors.
- Reducing the number of bugs in the codebase.
To be honest, the first benefit is not very convincing, as all decent editors offer decent autocomplete. What attracted me more are the other two potential benefits.
Does Static Typing Reduce Bugs
“How many public bugs could Flow and TypeScript have prevented if they had been in use when the bug committed?”.
Followed is a very short summary of how the study was conducted. The researchers took the code from a prior commit of commits that represent a bug fix from public repositories. They used static type annotations (with both Flux and TypeScript), and checked whether that would have fixed the bug fixed in the commit to follow (thus the bug not ending up in the repo in the first place). Taking a representative sample of 400 bugs from a number of different projects of varying sizes, the above-mentioned procedure was repeated with both “type checkers”.
As this is a very short summary of the paper and presents just the main findings, many details were omitted. As with any research paper, there are certain limitations and threats to validity, so I urge you to take the results with a grain of salt. Nevertheless, it is a good pointer towards the benefits of typing. I urge you to read the paper if you want to know the details on how the study was conducted.
If you still ask yourself whether you should be using static types or not, my answer is, as in almost all cases, it depends. In my opinion, anything bigger than a hobby project will benefit from types. I am not saying that it is impossible to build something of high quality without them, as there are quite a lot of projects suggesting the contrary, but I do think in the long term types will increase the robustness and confidence you have in your code.
A number of developers might state all sorts of arguments disagreeing with the results of the study presented, like how the people who introduced the bugs might have been inexperienced, the projects were not as widely used, and so on. The fact is, this is the reality of programming. Not everyone is closely familiar with a language, and not everyone has decades of experience. Finding ways to mitigate such risks is an important part of building robust software.