Do Programmers Prefer Predictable Expressions in Code?

Source code is a form of human communication, albeit one where the information shared between the programmers reading and writing the code is constrained by the requirement that the code executes correctly. Programming languages are more syntactically constrained than natural languages, but they are...

Full description

Saved in:
Bibliographic Details
Published inCognitive science Vol. 44; no. 12; pp. e12921 - n/a
Main Authors Casalnuovo, Casey, Lee, Kevin, Wang, Hulin, Devanbu, Prem, Morgan, Emily
Format Journal Article
LanguageEnglish
Published United States Wiley Subscription Services, Inc 01.12.2020
Wiley, Cognitive Science Society
Subjects
Online AccessGet full text

Cover

Loading…
More Information
Summary:Source code is a form of human communication, albeit one where the information shared between the programmers reading and writing the code is constrained by the requirement that the code executes correctly. Programming languages are more syntactically constrained than natural languages, but they are also very expressive, allowing a great many different ways to express even very simple computations. Still, code written by developers is highly predictable, and many programming tools have taken advantage of this phenomenon, relying on language model surprisal as a guiding mechanism. While surprisal has been validated as a measure of cognitive load in natural language, its relation to human cognitive processes in code is still poorly understood. In this paper, we explore the relationship between surprisal and programmer preference at a small granularity—do programmers prefer more predictable expressions in code? Using meaning‐preserving transformations, we produce equivalent alternatives to developer‐written code expressions and run a corpus study on Java and Python projects. In general, language models rate the code expressions developers choose to write as more predictable than these transformed alternatives. Then, we perform two human subject studies asking participants to choose between two equivalent snippets of Java code with different surprisal scores (one original and transformed). We find that programmers do prefer more predictable variants, and that stronger language models like the transformer align more often and more consistently with these preferences.
Bibliography:ObjectType-Article-1
SourceType-Scholarly Journals-1
ObjectType-Feature-2
content type line 14
content type line 23
National Science Foundation (NSF)
AC04-94AL85000; 1414172
SAND-2020-11603J
USDOE National Nuclear Security Administration (NNSA)
ISSN:0364-0213
1551-6709
1551-6709
DOI:10.1111/cogs.12921