What do developers consider magic literals? A smalltalk perspective - Inria - Institut national de recherche en sciences et technologies du numérique Accéder directement au contenu
Article Dans Une Revue Information and Software Technology Année : 2022

What do developers consider magic literals? A smalltalk perspective

Résumé

Context: Literals are constant values (numbers, strings, etc.) used in the source code. Magic literals are such values used without an explicit explanation of their meaning. Such undocumented values may hinder sourcecode comprehension, negatively impacting maintenance. Relatively little literature can be found on the subject beyond the usual (and very old) recommendation of avoiding literals and preferring named constants. Yet, magic literals are still routinely found in source code. Objective: We studied literal values in source code to understand when they should be considered magic or not (i.e., acceptable). Methods: First, we perform a qualitative study of magic literals, to establish why and under which conditions they are considered harmful. We formalize hypotheses about the reasoning behind how literals are considered magic. Second, we perform a quantitative study on seven real systems ranging from small (a few classes) to large (thousands of classes). We report the literals' types (number, string, Boolean,. . .), their grammatical function (e.g., argument in a call, operand in an expression, value assigned,. . .), or the purpose of the code in which they appear (test methods, regular code). Third, we report on another study involving 26 programmers who analyzed about 24,000 literals, to understand which ones they consider magic. Finally, we evaluate the hypotheses defining specific conditions under which literals are acceptable. Results: We show that (1) literals still exist and are relatively frequent (found in close to 50% of the methods considered); (2) they are more frequent in test methods (in 80% of test methods); (3) to a large extent, they were considered acceptable (only 25% considered magic); and (4) the hypotheses concerning acceptable literals are valid to various degrees. Conclusion: We thus pave the way to future research on magic literals, for example, with tools that could help developers deciding if a literal is acceptable.
Fichier principal
Vignette du fichier
Anqu22a-infsof106942-MagicLiterals-Official.pdf (847.62 Ko) Télécharger le fichier
Origine : Fichiers produits par l'(les) auteur(s)

Dates et versions

hal-03679130 , version 1 (31-05-2022)

Identifiants

Citer

Nicolas Anquetil, Julien Delplanque, Stéphane Ducasse, Oleksandr Zaitsev, Christopher Fuhrman, et al.. What do developers consider magic literals? A smalltalk perspective. Information and Software Technology, 2022, 149, ⟨10.1016/j.infsof.2022.106942⟩. ⟨hal-03679130⟩
65 Consultations
109 Téléchargements

Altmetric

Partager

Gmail Facebook X LinkedIn More