views:

413

answers:

5

There are many different styles of variable names that I've come across over the years.

The current wikipedia entry on naming conventions is fairly light...

I'd love to see a concise catalog of variable naming-conventions, identifying it by a name/description, and some examples.

If a convention is particularly favored by a certain platform community, that would be worth noting, too.

I'm turning this into a community wiki, so please create an answer for each convention, and edit as needed.

+1  A: 

Sun published a list of variable name conventions for Java here. The list includes conventions for naming packages, classes, interfaces, methods, variables, and constants.

The section on variables states

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.

Some examples include

int             i;
char            c;
float           myWidth;
Bill the Lizard
+2  A: 

The best naming convention set that I've seen is in the book "Code Complete" Steve McConnell has a great section in there about naming conventions and lots of examples. His examples run through a number of "best practices" for different languages, but ultimately leave it up to the developer, dev manager, or architect to decide the specific action.

Joe Basirico
+1  A: 

PEP8 is the style guide for Python code that is part of the standard library which is relatively influential in the Python community. For bonus points, in addition to covering the naming conventions used by the Python standard library, it provides an overview of naming conventions in general.

According to PEP8: variables, instance variables, functions and methods in the standard library are supposed to use lowercase words separated by underscores for readability:

foo
parse_foo
a_long_descriptive_name

To distinguish a name from a reserved word, append an underscore:

in_
and_
or_

Names that are weakly private (not imported by 'from M import *') start with a single underscore. Names whose privacy is enforced with name mangling start with double underscores.:

_some_what_private
__a_slightly_more_private_name

Names that are Python 'special' methods start and end with double underscores:

__hash__  # hash(o) = o.__hash__()
__str__   # str(o) = o.__str__()
Aaron Maenpaa
+1  A: 

Whatever your naming convention is, it usually do not allow you to easily distinguish:

  • instance (usually private) variable
  • local variables (used as parameters or local to a function)

I use a naming convention based on a the usage of indefinite article (a, an, some) for local variables, and no article for instance variables, as explained in this question about variable prefix.

So a prefix (either my way, or any other prefix listed in that question) can be one way of refining variable naming convention.

I know your question is not limited on variables, I just wanted to point out that part of naming convention.

VonC
A: 

There is, the "Java Programmers Phrasebook" by Einar Hoest.

He analysed the grammatical structure of method names together with the structure of the method bodies, and collected information such as:

add-[noun]-* These methods often have parameters and create objects. They very rarely return field values. The phrase appears in practically all applications.

etcetera ... collected from hundreds of open-source projects.

For more background information, please refer to his SLE08 paper.

Adrian