arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2410.23360cs.SE
收藏

开发者如何使用类型推断:一项基于Kotlin的探索性研究

How Do Developers Use Type Inference: An Exploratory Study in Kotlin

Samuel W. Flint, Ali M. Keshk, Robert Dyer, Hamid Bagheri

AI总结:

本研究针对支持类型推断的Kotlin语言,用Boa工具分析49万余个项目的类型推断使用模式,发现局部变量及外部方法初始化的变量更常用类型推断,为语言设计与算法优化提供参考。

摘要:

静态类型语言为开发者带来诸多益处,例如提升代码质量、减少运行时错误,但也带来了手动编写类型注解的开销。为减轻这一负担,语言设计者开始加入类型推断支持,即编译器根据变量的声明与使用上下文推断其类型。由此,类型注解在特定场景下成为可选项,开发者可在这些场景中使用类型推断。然而,在支持类型推断的语言中,类型注解的使用模式尚不明确,这些模式能为程序理解、语言设计及教育领域的后续研究提供实证依据。\n我们使用软件仓库挖掘工具Boa开展了一项大规模实证研究,针对498963个Kotlin项目,调查开发者在何时、何地使用类型推断。我们选择Kotlin是因为它是Android开发的默认语言,而Android是最大的软件市场之一;此外,Kotlin从首个版本起就支持声明处的可选类型注解。\n研究结果表明,类型推断常用于局部变量,且由文件外部声明的方法调用初始化的变量更可能使用类型推断。这些结果对语言设计者具有重要意义,可为哪些场景允许类型推断、如何优化类型推断算法以实现最高效率提供宝贵见解,最终改善开发者的开发体验。

英文摘要:

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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑