How do annotations affect Java code readability?

Eduardo Guerra,Everaldo Gomes,Jeferson Ferreira,Igor Wiese,Phyllipe Lima,Marco Gerosa,Paulo Meirelles
2024-04-26
Abstract:Context: Code annotations have gained widespread popularity in programming languages, offering developers the ability to attach metadata to code elements to define custom behaviors. Many modern frameworks and APIs use annotations to keep integration less verbose and located nearer to the corresponding code element. Despite these advantages, practitioners' anecdotal evidence suggests that annotations might negatively affect code readability. Objective: To better understand this effect, this paper systematically investigates the relationship between code annotations and code readability. Method: In a survey with software developers (n=332), we present 15 pairs of Java code snippets with and without code annotations. These pairs were designed considering five categories of annotation used in real-world Java frameworks and APIs. Survey participants selected the code snippet they considered more readable for each pair and answered an open question about how annotations affect the code's readability. Results: Preferences were scattered for all categories of annotation usage, revealing no consensus among participants. The answers were spread even when segregated by participants' programming or annotation-related experience. Nevertheless, some participants showed a consistent preference in favor or against annotations across all categories, which may indicate a personal preference. Our qualitative analysis of the open-ended questions revealed that participants often praise annotation impacts on design, maintainability, and productivity but expressed contrasting views on understandability and code clarity. Conclusions: Software developers and API designers can consider our results when deciding whether to use annotations, equipped with the insight that developers express contrasting views of the annotations' impact on code readability.
Software Engineering
What problem does this paper attempt to address?
This paper explores the impact of code annotations on the readability of Java code. The study presents 15 pairs of Java code snippets with and without annotations based on five real-world Java frameworks and API categories that utilize annotations. The survey results demonstrate that developers have a wide range of preferences for all categories of annotation usage, indicating a lack of consensus on the impact of annotations on code readability. While some developers believe that annotations can improve design, maintainability, and productivity, others think that annotations may decrease the understandability and clarity of the code. The paper points out that although some projects excessively use annotations, and an excessive number of identifiers can harm readability, analyzing code evolutions shows that Java code with annotations tends to have fewer errors. This raises the question of whether annotations have a positive effect on maintainability due to their impact on readability. Developers and API designers should consider the different perspectives of developers on the impact of annotations on readability when deciding whether to use annotations. The main contribution of the research is to investigate how annotations affect code readability among different groups of developers and in different usage scenarios, providing guidance for API designers in deciding whether to use annotations or traditional object-oriented APIs. A secondary contribution is evaluating the frequency of usage categories for annotations, offering a reference for API usage in open-source projects.