Attention: open in a new window. PDFPrint

Frank’s PHP, JavaScript, C#, VB, and Java Variable Naming Conventions
Last Updated 2009-02-03d
Room4me.com Software LLC

Working with the syntax of multiple languages can get tricky.  Having a consistent way to name program elements across those languages helps.  When given the freedom to name variables any way I want, this is what I choose with almost no variation across languages:

  1. With very rare exception, my variable names are never plural.  (This is also a good strategy for database element names.)
  2. Except for member variables, there are no underscores.
  3. Member variables either start with “m_” or just “_” prefix.
  4. All enumeration declarations end with an “Enum” suffix.
  5. All variables are given a functional datatype related single character prefix. This is not Hungarian notation , but serves a similar purpose in a much simpler way.

Variable Name Prefix Conventions

Unlike Hungarian notation, all prefixes are a single lowercase letter. And prefix choice is based on general datatype family group of the variable rather than explicit language data type. For example, integer has the same prefix as a long, which is the same as a float, etc.

Functional Datatype

Prefix

Example

Any kind of number (e.g., int, long, float, etc)

n

nCalcCategory

Any string or character variable

s

sCalcCategory

Any array

a

aCalcCategory

Any collection class instance (e.g., ArrayList, List, Hashtable, etc)

k

kCalcCategory

Any class instance (object) that is not a collection class instance.

o

oCalcEngine

Any variable that holds an enumerated value

e

eCalcCategory

Any true/false (boolean) variable.

b

bCalculate

So for a local int variable we have the name nTotal, but if it is a member variable we have the name _nTotal or m_nTotal.

Constants

I try to declare constants in ALL_CAPITAL_LETTERS with underscores between words. This does not break any rules because a constant is not really a variable in my book. Sometimes I do put an “s” or an “n” prefix in front of the constant though.


Other Conventions Worth Mentioning

For Java, PHP, C, and C++ I like to name my classes starting with an uppercase word and all methods starting with a lowercase word. This is pretty much the Java coding standard as defined by Sun. Some C/C++ libraries follow it, some don’t.

For VB and C# I tend to name methods starting with a capital letter too just because there is too much code already out there that was created that way. Also, it seems to be the Microsoft convention. When in Rome and all that stuff.

Braces

I also find it easier to read code when the opening French brace “{“ starts on a new line rather than at the end of a line. My impatient eyes like to match simple things vertically, especially when there is deep nesting.

Enumerations

The name of an enumeration is always suffixed with “Enum”. The rest of the name follows the same rule used for class names. So an enumeration of Calculation Categories might be called “CalcCategoryEnum”. The suffix on the enumeration name is nice because it distinguishes those names from class names at a glance.

Why Have any Prefixes and Suffixes?

Prefixes and suffixes are a crutch for a less than magnificent memory. I started coding in the days where you debugged on paper and hovering over a variable in your editor did not popup the data type. The prefixes made good memory joggers then and still do today when getting into the weeds of a long listing on paper.

Even with modern IDE environments I find it quicker to visually scan a screen full of statements to glean what is happening using simple and consistent prefix and suffix clues than I do when I have to parse the entire variable names or worse, move the mouse pointer to hover and see the definition. I like coding fast and prefixes feed my impatient nature.

Why Like This and not Richer like Hungarian Notation?

I don’t like prefixing schemes that require too much instruction or overwhelm the variable name. This one is short and is understandable to a knowledgeable programmer without sitting for a seminar. Programmers can pick up on the pattern even without an explanation.