Chuniversiteit.nl
The Toilet Paper

Why snake_case is better than camelCase

There aren’t many reasons why you should choose snake_case over camelCase, but the ones that are there are pretty convincing.

A snake is strangling a camel
The snake was like a straw that broke the camel’s back

Developers spend a lot of time reading code, much of which consists of identifiers. As we saw last week, the identifier style (snake_cased or camelCased) may affect the speed and correctness at which developers read code.

One year later, some other researchers attempted to replicate the experiment from the original study, with a few adjustments.

In the original experiment participants were first shown a two- or three-word phrase in a screen. The next screen would then show four “clouds” that randomly moved around the screen. One cloud contained the corresponding snake_cased or camelCased version of the phrase, while the other three contained versions that differed ever so slightly. This process was repeated several times with multiple phrases.

Several adjustments were made for the replication study. The most notable changes include:

  • the experiment uses fewer phrases;
  • the sample is smaller, but also more balanced and representative;
  • the clouds no longer move around the screen;
  • an eye tracker was set up to monitor subjects’ eye movements.

The idea behind the use of an eye tracker is that visual attention (i.e. focussing the eyes on a particular location) can be seen as a sign that a subject is mentally processing information about whatever it is they are looking at.

There are mainly two types of eye gaze data: eye fixations, when the eye focusses on an object of interest for a period of time, and saccades, which are jerky eye movements that occur between two fixations. Comprehension mainly happens during fixations.

Results

Fifteen volunteers from Kent State University participated in the study: seven undergraduate students, eight graduate students, and two faculty members.

Virtually all tasks were completed successfully. . Other than that, there was no significant difference in correctness between snake_cased and camelCased phrases.

But just like in the original experiment, camelCased phrases took more time to read, especially for longer identifiers that consist of many parts. Expert code readers are not affected as much by identifier style as novices, although the effect is still present in both groups.

The eye tracker data show that both the number and length of fixations are higher for camelCased than for snake_cased identifiers. This suggests that a longer mental parsing time is needed to parse camelCased identifiers.

Summary

  1. CamelCased identifiers take more time and mental effort to read than snake_cased identifiers

  2. CamelCased identifiers are more easily misread, especially when they consist of three or more words