[论文解读] To Type or Not to Type? A Systematic Comparison of the Software Quality of JavaScript and TypeScript Applications on GitHub
本研究通过 SonarQube 和 GitHub API,对 604 个 GitHub 上的 JavaScript 和 TypeScript 仓库(代码量超过 1600 万行)进行了软件质量比较。研究发现,TypeScript 提升了代码质量和可理解性,但并未减少引入缺陷的倾向或缩短缺陷修复时间——出人意料的是,JavaScript 项目的缺陷指标表现更优,且减少 'any' 类型的使用与更高的质量相关。
JavaScript (JS) is one of the most popular programming languages, and widely used for web apps and even backend development. Due to its dynamic nature, however, JS applications often have a reputation for poor software quality. As a type-safe superset of JavaScript, TypeScript (TS) offers features to address this. However, there is currently insufficient empirical evidence to broadly support the claim that TS apps exhibit better software quality than JS apps. We therefore conducted a repository mining study based on 604 GitHub projects (299 for JS, 305 for TS) with over 16M LoC and collected four facets of software quality: a) code quality (# of code smells per LoC), b) code understandability (cognitive complexity per LoC), c) bug proneness (bug fix commit ratio), and d) bug resolution time (mean time a bug issue is open). For TS, we also collected how frequently the type-safety ignoring `any` type was used. The analysis indicates that TS apps exhibit significantly better code quality and understandability than JS apps. Contrary to expectations, however, bug proneness and bug resolution time of our TS sample were not significantly lower than for JS: mean bug fix commit ratio was more than 60% larger (0.126 vs. 0.206), and TS projects needed on average more than an additional day to fix bugs (31.86 vs. 33.04 days). Furthermore, reducing the usage of the `any` type in TS apps was significantly correlated with all metrics except bug proneness (Spearman's rho between 0.17 and 0.26). Our results indicate that the perceived positive influence of TypeScript for avoiding bugs in comparison to JavaScript may be more complicated than assumed. While using TS seems to have benefits, it does not automatically lead to less and easier to fix bugs. However, more research is needed in this area, especially concerning the potential influence of project complexity and developer experience.
研究动机与目标
- 通过实证评估 TypeScript (TS) 是否相比 JavaScript (JS) 提升了软件质量,回应 TS 优势的普遍信念。
- 解决现有开源仓库中 JS 与 TS 应用之间缺乏大规模、直接实证比较的问题。
- 探究 TS 中的静态类型是否导致缺陷更少、修复更快,与常见假设相反。
- 考察在 TS 中使用 'any' 类型对软件质量指标的影响。
- 为未来基于真实世界数据的语言选择与软件质量研究,提供透明且可复现的基础。
提出的方法
- 从 604 个 GitHub 仓库(299 个 JS,305 个 TS)中收集数据,代码量超过 1600 万行,确保项目规模均衡以控制项目大小的影响。
- 使用 SonarQube 测量代码质量(每千行代码的代码异味数量)和每千行代码的认知复杂度,作为可理解性的代理指标。
- 通过在提交信息中检测关键词 'bug' 和 'fix',自动化识别缺陷修复提交,并在 0.5% 的随机样本(500 个提交)上进行验证。
- 使用 GitHub API 测量缺陷修复时间,定义为从问题创建到最后一次评论的平均时间(依据 Zheng 等人,2015 年)。
- 使用 ESLint 搭配 typescript-eslint 插件,统计 TS 项目中 'any' 类型的使用频率,评估其对质量指标的影响。
- 应用统计分析(如 t 检验、Spearman 等级相关系数)比较 JS 与 TS 在四个质量维度上的表现,并分析 'any' 类型使用与结果的相关性。
实验结果
研究问题
- RQ1在真实世界的开源项目中,TypeScript 是否显著优于 JavaScript,表现出更好的代码质量和可理解性?
- RQ2与 JavaScript 相比,TypeScript 是否与更低的缺陷易感性(更少的缺陷修复提交)相关?
- RQ3与 JavaScript 相比,TypeScript 是否导致更短的缺陷修复时间?
- RQ4在 TypeScript 中使用 'any' 类型在多大程度上与软件质量指标相关?
- RQ5是否存在混淆因素(如项目复杂度或开发人员经验),可能解释 JS 与 TS 在缺陷相关指标上的差异?
主要发现
- TypeScript 应用的代码质量显著更优,每千行代码的代码异味数量比 JavaScript 少 18.5%(p < 0.01)。
- TypeScript 项目的认知复杂度显著更低(平均每千行代码 1.23 vs. 1.35),表明代码可理解性更高。
- JavaScript 项目的缺陷修复提交比率低 60%(0.126 vs. 0.206),表明 JS 应用每千行代码的缺陷修复提交更少。
- TypeScript 项目的缺陷修复时间平均为 33.04 天,长于 JavaScript 项目的 31.86 天,尽管差异较小。
- 'any' 类型在 TypeScript 中的使用频率与更差的代码质量和可理解性显著相关(rho = 0.17–0.26),但与缺陷易感性无关。
- 尽管存在相关性,效应量较小,表明虽然 'any' 类型的使用会损害质量,但其影响有限,不是缺陷指标的主要驱动因素。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。