The Artima Developer Community
Sponsored Link

Angle Brackets and Curly Braces
Are Shorter Variable Names Better?
by Bill Venners
August 28, 2009
Summary
A recent paper by Gilles Dubochet describes a study that concluded Scala code written in a functional style that favors for expressions and higher order functions is easier to comprehend than Scala code written in a more Java-like, looping style. It also found that meaningful variable names were insigificant or even detrimental to comprehension.

Advertisement

A recent paper by Gilles Dubochet, Computer Code as a Medium for Human Communication: Are Programming Languages Improving?, measured eye movements of people reading Scala code written in three different styles: a Java-like, looping style and two versions of a more idiomatic Scala style that used for expressions and higher-order functions instead of loops: one with meaningful variable names and one without. Although I was not surprised to see that the more verbose looping-style code took longer and/or was harder to comprehend than the more concise functional style code, I was not sure what to make of the difference between the two variable naming styles.

In one style, called grounded in the paper, intermediate functions and variables were given domain-meaningful names. A few intermediate variables were added as well, and given meaningful names to help readers connect each intermediate value to its domain meaning. In the other style, called ungrounded in the paper, concentrated solely on cleanly expressing the algorithm using as few intermediate names as possible, and always using meaningless words.

The results seem to show that the meaningless variable name version was easier and/or quicker to comprehend than the one with domain-meaningful variable names. One caveat they point out in the paper is that each participant in the study was made familiar with the domain before letting them see the code. It may be that people not familiar with the domain would have had an easier time figuring out the version with domain-meaningful names, however, in general I'd guess programmers reading code are usually familiar with the domain of that code to at least some extent.

A wise programmer once told me that the way he recommends people name variables is he asks them what the variable is, and whatever they answer, he tells them that's what the name should be. I tend to use descriptive names, but sometimes feel longer names have a tradeoff of cluttering the code, giving it a weight that takes away from seeing the structure of the code.

How do you decide what to name variables? When do you use single character names for variables? When do you use short abbreviations (but longer than one character)? And when do you use full words?

Talk Back!

Have an opinion? Readers have already posted 13 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Bill Venners adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Bill Venners is president of Artima, Inc., publisher of Artima Developer (www.artima.com). He is author of the book, Inside the Java Virtual Machine, a programmer-oriented survey of the Java platform's architecture and internals. His popular columns in JavaWorld magazine covered Java internals, object-oriented design, and Jini. Active in the Jini Community since its inception, Bill led the Jini Community's ServiceUI project, whose ServiceUI API became the de facto standard way to associate user interfaces to Jini services. Bill is also the lead developer and designer of ScalaTest, an open source testing tool for Scala and Java developers, and coauthor with Martin Odersky and Lex Spoon of the book, Programming in Scala.

This weblog entry is Copyright © 2009 Bill Venners. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us