CleanCode logo
NAVIGATION: first page in sectionprevious pageup one levelnext pagefinal page in section

Guideline NM2: Add clarity with consistent naming conventions

Naming Conventions
one-word constantCAPS
multi-word constantCAPS_WITH_UNDERSCORES
local one-word variables, functions, methodslower
local multi-word variables, functions, methodslowerThenCapitalize
global one-word classes, variables, functions, methodsCapitalize
global multi-word classes, variables, functions, methodsCapitalizeEachWord
private variables, functions, methods (non-Java)_startWithUnderscore
private variables, functions, methods (Java)private keyword
pseudo-package identifiers (JavaScript)File_Package_methodOrFunctionOrVariable

Naming conventions is a topic dealt, by many people, with zealous fervor. Perhaps overzealous. But I shall sally forth and muck about with it nonetheless.

In the table at left, I've attempted to illustrate the syntax of each entity with names which themselves describe the syntax. And, except where noted, they are language-independent. As usual on the CleanCode website, I will predominately talk about Java, Perl, and JavaScript. But guidelines may often be applied to other languages; the pseudo-package identifier notation or private variable notation could be used in C, for example.

The most controversial, perhaps is the multi-word variable name; I've shown lowerThenCapitalize and CapitalizeEachWord in the table. This style has been promoted by Java, one of the first languages to strongly advocate conventions for everybody. Standards in any industry, make it easier to go from one brand to another, or in our case, from one person's code to another. Imagine if each computer had its own custom CDROM format--the world would be a lot uglier, wouldn't it? Or what if--this is really far-fetched, I know--every operating system had a different format for executable programs? Then it would be impossible to run a Windows program on Linux or Macintosh or... Whoops. That is what we've got today, isn't it? In large part, yes, but not with Java. Java executable files will run on any computer with a Java virtual machine.

But back to variable names. Some people will strongly advocate a_name_like_this, insisting that run together words, even with capitals in between, are difficult to read. My preference is for the intermediate capitals because it is promoted by Java, and because it meshes nicely with the pseudo-package identifier technique shown in the table. See NM1 for a discussion of the pseudo-package technique in JavaScript.

Valid XHTML 1.0!Valid CSS!Get CleanCode at Fast, secure and Free Open Source software downloads
Copyright © 2001-2013 Michael Sorens • Contact usPrivacy Policy
Usage governed by Mozilla Public License 1.1 and CleanCode Courtesy License
CleanCode -- The Website for Clean DesignRevised 2013.06.30