Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you use articles in your variable names?

Edit: There appears to be at least two valid reasons why Smalltalkers do this (readability during message chaining and scoping issues) but perhaps the question can remain open longer to address general usage.

Original: For reasons I've long forgotten, I never use articles in my variable names. For instance:

aPerson, theCar, anObject

I guess I feel like articles dirty up the names with meaningless information. When I'd see a coworker's code using this convention, my blood pressure would tick up oh-so-slightly.

Recently I've started learning Smalltalk, mostly because I want to learn the language that Martin Fowler, Kent Beck, and so many other greats grew up on and loved.

I noticed, however, that Smalltalkers appear to widely use indefinite articles (a, an) in their variable names. A good example would be in the following Setter method:

name: aName address: anAddress.
     self name: aName.
     self address: anAddress

This has caused me to reconsider my position. If a community as greatly respected and influential as Smalltalkers has widely adopted articles in variable naming, maybe there's a good reason for it.

Do you use it? Why or why not?

like image 636
Robert Campbell Avatar asked Apr 09 '09 12:04

Robert Campbell


People also ask

What are the 4 rules for variable names?

Variable names are case-sensitive (age, Age and AGE are three different variables) There is no limit on the length of the variable name. A variable name cannot contain spaces. The variable name cannot be any Go keywords.

What are the rules for variable names?

Rules for naming a variableA variable name can only have letters (both uppercase and lowercase letters), digits and underscore. The first letter of a variable should be either a letter or an underscore.


1 Answers

This naming convention is one of the patterns in Kent Beck's book Smalltalk Best Practice Patterns. IMHO this book is a must-have even for non-smalltalkers, as it really helps naming things and writing self-documenting code. Plus it's probably one of the few pattern langages to exhibit Alexander's quality without a name.

Another good book on code patterns is Smalltalk with Style, which is available as a free PDF.

Generally, the convention is that instance variables and accessors use the bare noun, and parameters use the indefinite article plus either a role or a type, or a combination. Temporary variables can use bare nouns because they rarely duplicate the instance variable; alternatively, it's quite frequent to name them with more precision than just an indefinite article, in order to indicate their role in the control flow: eachFoo, nextFoo, randomChild...

like image 154
Damien Pollet Avatar answered Sep 28 '22 08:09

Damien Pollet