Statically typed languages offer numerous benefits to developers, such as improved code quality and reduced runtime errors, but they also require the overhead of manual type annotations. To mitigate this burden, language designers have started incorporating support for type inference, where the compiler infers the type of a variable based on its declaration/usage context. As a result, type annotations are optional in certain contexts, and developers are empowered to use type inference in these situations. However, the usage patterns of type annotations in languages that support type inference are unclear. These patterns can help provide evidence for further research in program comprehension, in language design, and for education. We conduct a large-scale empirical study using Boa, a tool for mining software repositories, to investigate when and where developers use type inference in 498,963 Kotlin projects. We choose Kotlin because it is the default language for Android development, one of the largest software marketplaces. Additionally, Kotlin has supported declaration-site optional type annotations from its initial release. Our findings reveal that type inference is frequently employed for local variables and variables initialized with method calls declared outside the file are more likely to use type inference. These results have significant implications for language designers, providing valuable insight into where to allow type inference and how to optimize type inference algorithms for maximum efficiency, ultimately improving the development experience for developers.
翻译:静态类型语言为开发者提供了诸多优势,例如提升代码质量与减少运行时错误,但同时也带来了手动添加类型标注的开销。为减轻这一负担,语言设计者开始引入类型推断支持,即编译器根据变量声明/使用上下文自动推断其类型。这使得在某些上下文中类型标注成为可选项,开发者得以在这些场景中运用类型推断功能。然而,在支持类型推断的语言中,类型标注的实际使用模式尚不明确。厘清这些模式能为程序理解、语言设计与教育领域的进一步研究提供实证依据。我们利用软件仓库挖掘工具Boa对498,963个Kotlin项目展开大规模实证研究,探究开发者在何时何地使用类型推断。选择Kotlin是因为其作为Android开发(全球最大的软件市场之一)的默认语言,且自初始版本就支持声明处可选类型标注。研究发现:类型推断常被用于局部变量;通过文件外部声明的方法调用初始化的变量更倾向于使用类型推断。这些发现对语言设计者具有重要启示,既为允许类型推断的适用场景提供依据,也为优化类型推断算法以实现最高效率指明方向,最终将提升开发者的编程体验。