The topic of code style and readability is a challenging one. Today, I will discuss two papers on this topic. Beyond just a subjective sense of what is easy to read and understand, I wondered if there were more scientific studies on the matter.
Table of Contents
Study 1: The Influence of Reading Speed and Line Length on Screen
The first study was conducted in 2001 with support from Microsoft. It aimed to investigate the differences in comprehension based on reading speed (normal, fast) and line length (25, 55, 100 characters). While there were many similar studies on printed materials, research on screen-based text in the digital era was relatively scarce.
Previous research on printed material suggested that a line length under 70 characters was preferable. This study also aimed to see if the same principal applied to screen reading. The study measured comprehension by varying the line length and reading speed of the text presented, then asking questions related to the content. Since we are primarily concerned with line length here, we will focus on that aspect of the study’s findings. Below is an example of the screen used in the study to measure comprehension.
The study found that overall comprehension decreases as line length increases. At normal reading speeds, comprehension clearly decreased as line length increased, and at fast reading speeds, comprehension was relatively lower for both very short and very long lines.
An interesting point is that, irrespective of reading speed, the amount of text read in a specific time also increased as the characters per line (cpl) increased, as can be seen in the figure below.
In summary, while longer lines can be read more quickly, benefiting skimming, they reduce actual comprehension. The study hypothesized that this correlation between line length and comprehension might result from the natural movement of the eyes and the challenge of finding the start of the next line. The eyes have a natural movement range, and longer lines can cause eye movement beyond this range, thus hindering reading.
Additionally, it’s harder to find the start of the next line in longer lines, which can also impact comprehension. The study speculated that the decrease in comprehension at fast reading speeds with short line lengths might be due to the difficulty of gathering information in a single focus and the need to scroll more frequently.
Study 2: The Impact of Coding Conventions on Readability
Another study, conducted more recently in 2018, sought to understand the impact of coding conventions on readability. The study involved showing participants Java code that either adhered to or violated specific coding conventions, and asked the participants to choose which they found more readable.
The participants were developers, with years of experience distributed as shown below.
Eleven coding conventions were evaluated, including:
1. Space before opening brace
// Adherent
if(hi) {
…
}
// Violating
if(hi){
…
}
2. Brace following statement on the same line
// Adherent
while(hi) {
…
}
// Violating
while(hi)
{
…
}
3. Using a blank line to separate related code
// Adherent
var other = otherService.getOne();
other.doWork();
cache.put(other);
repository.save(other);
// Violating
var other = otherService.getOne();
other.doWork();
cache.put(other);
repository.save(other);
4. Limiting line length to 80 characters
5. Indentation of 4 spaces
// Adherent
if(hi) {
doSmth();
}
// Violating
if(hi) {
doSmth();
}
6. Code blocks not exceeding 3 levels of depth
7. Only one semicolon per line
// Adherent
doSmth();
doOther();
// Violating
doSmth(); doOther();
8. Avoiding canonical names
// Adherent
@Service
// Violating
@org.springframework.stereotype.Service
9. Not chaining property calls
10. Separating long operations into local variables
11. Using dictionary words for naming
As you can see from above result, eight of these conventions showed statistically significant results. Seven had a positive impact and one a negative impact on readability. Conventions 1, 4, 7, 8, 9, and 11 improved readability, while convention 2 hindered it. The study attributed the results for convention 2 to participants’ prior experience with C#.
Conclusion
The studies demonstrate that code style does impact readability. The first study’s insights into screen reading reveal that shorter line lengths enhance comprehension, emphasizing the balance between information presentation and cognitive processing. The second study in the coding context highlights how adherence to coding conventions, especially in line length, indentation, and naming, improves readability, albeit influenced by the reader’s background.
In essence, these studies confirm that code style is not mere aesthetics but a vital component of effective communication. It’s crucial for developers to understand and apply these principles to make their work not only functional but also accessible and comprehensible. Embracing these insights can lead to clearer, more efficient coding practices.
References
Dyson, Mary & Haselgrove, Mark. (2001). The influence of reading speed and line length on the effectiveness of reading from screen. Retrieved from https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.108.4346&rep=rep1&type=pdf
Santos, Rodrigo & Gerosa, Marco. (2018). Impacts of Coding Practices on Readability. Retrieved from https://www.ime.usp.br/~gerosa/p