Table of Contents
Fetching ...

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

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

TL;DR

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 reveals 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.

Abstract

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.

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

TL;DR

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 reveals 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.

Abstract

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.

Paper Structure

This paper contains 39 sections, 7 figures, 8 tables, 4 algorithms.

Figures (7)

  • Figure 1: Example type inference locations in Kotlin. Developers can omit code in gray and the compiler will try to infer the type. Type annotations in red do not support inference and must be supplied by the developer.
  • Figure 2: Usage of type inference per inferable location (medians of each category sum to roughly 100%), outliers not shown. See also \ref{['tab:type-inf-place']}.
  • Figure 3: Usage of type inference (as percentage) in all locations of a project (all medians sum to roughly 100%), outliers not shown.
  • Figure 4: Most common expression types in variable initializers (per project).
  • Figure 5: Per-project percent of type inference usage by location and mutability (outliers not shown).
  • ...and 2 more figures