To Type or Not to Type?
TL;DR
[BognerMerkel2022] examined TypeScript and JavaScript-based public GitHub projects and measured Code Quality, Understandability, Bug proneness and Bug resolution time. They did find that TypeScript projects are weakly correlated with better code quality and understandability but not less bug proneness. Their study remains inconclusive since they didn't account for many influencing factors.
Introduction
We need a rational way to choose the right toolset and processes in software engineering. One way is to find repeatable patterns by analyzing many projects and seeing what the data tells us. Hence, I think it is essential to read about current research in software engineering, and this is why I wrote this summary on the paper "A Systematic Comparison of the Software Quality of JavaScript and TypeScript Applications on GitHub" by [BognerMerkel2022].
In the developer community, the notion is that Typescript eliminates whole classes of errors. Moreover, Typescript is the preferred choice for writing web applications in enterprise contexts. In the "2021 State of JS" survey released on February 15, 69% of respondents answered they use Typescript. Similar tools are becoming popular in other languages as well, for example, in Python with the rise of mypy. The paper by [BognerMerkel2022] focuses on comparing Typescript with JavaScript applications. In JavaScript, "[a] single change to one individual file can affect the behaviour of any number of other files, like throwing a pebble into a pond and causing ripples to spread out to the bank" [1]. Typescript automatically validates the connections between every part of a project and provides instant feedback during development by statically identifying constructs that are likely to be errors [2]. Because of the earlier feedback developers get, we could assume that Typescript apps are of higher quality than their JavaScript counterparts. However, this assessment has not been verified by looking at what works for teams. Thus, [BognerMerkel2022] analyzed over 604 GitHub projects to verify if TypeScript applications exhibit better software quality than JavaScript applications. They defined software quality along four dimensions (accounted for project size):
- Code Quality: The more code smells they found, the lower the quality of the code (measured with SonarQube).
- Code understandability: Calculated via cognitive complexity (measured with SonarQube).
- Bug proneness: Frequency of bug fix commits in the git history of the respective project, [ZhangEtAl2019].
- Bug resolution time: Measured via the GitHub issue tracking system (how long it took to close an issue labelled with "bug").
Results
They did find more code smells per line of code in JavaScript applications: "[...] roughly 12 code smells per 1 LoC, more than TS apps". Additionally, they observed that "TypeScript applications have [...] lower cognitive complexity than JavaScript applications, i.e., better code understandability." Moreover, "TypeScript applications have a higher bug fix commit ratio than JavaScript applications, i.e., are more or equally prone to bugs". Other than that, "TypeScript applications take more or equal time in bug resolution than JavaScript applications". Frequent usage of the any type is correlated with worse code quality and a higher bug resolution time. In the discussion section, they list multiple possible explanations for their discoveries:
- Typescript code could have better quality because professional developers mainly use it. Experienced developers are more likely to use static analysis tools like ESLint and thus have fewer code smells.
- Typescript applications could have more bugs because typescript developers tend to document bugs more thoroughly.
- Projects with high domain complexity could choose TypeScript more for projects with high domain complexity.
Discussion
As [BognerMerkel2022] states in the "Threats to validity" section, they can't show that using Typescript leads to fewer bugs which one would assume if we say that Typescript eliminates whole classes of errors. While they account for project size as a confounding variable they miss on others, such as domain complexity, the experience level of programmers and how thoroughly issues are tracked. However, their work does show that using Typescript correlates with higher code quality. Although, I'm curious about how well SonarQube works for measuring code quality. To bring in another critical perspective, I can recommend reading Bruce Eckel's piece where he argues against the need for static typing (in the case of Python) here: Strong Typing vs Strong Testing. In summary, his opinion is that a Python program with good unit tests can be as robust as a statically typed one.