Java Naming Convention - JNC

Rules for variables and objects naming suggested to the Java developers community

When I began to learn Java I was in shock - such popular programming language didn't have a naming convention! OK, it had a few rules (the first letter is lower-case in variables names and upper-case in classes/interfaces names, each subsequent word in names begins with an upper-case letter, exceptions should end with "Exception", interfaces should end with "...able"), but these rules were not a real naming convention. It was better than nothing, but... it was almost nothing! By then I had been working for 8 years in PowerBuilder which had a very quality naming convention, and I didn't imagine my work without such a helper... Being an independent contractor, I was moving from project to project between different cities of the world, and in each script of each application I saw the same: if a variable name began with a prefix "li" - it was a local integer variable, "al" told me that I saw an argument of type long, and a variable prefixed "iw" had no chance to be something different from an instance pointer to a window. A professional detective sees in each thing much more than ordinary people do, but PowerBuilder developers (as distinct from their Java colleagues) don't need to have the detective talent to tell a whole story just after a short look at a variable: what is its data type, where it is declared, when it was born and when it will die... Learning Java, I was missing that "system of secret spy signs", so before writing my first project in that language, I decided to adopt for it the PowerBuilder naming convention. In this page you can see the results of that small effort - please use the convention in you projects if you like it! And if you will give a link to this page to your manager then don't forget to highlight that using of a deeply pre-thought naming convention like this one makes programs less bugs-prone and really saves hours and days of development! Added prefix - saved money!


Michael Zuskin, Calgary, Canada


/******************************************************************************/
/****** VARIABLES OF PRIMITIVE TYPES AND THEIR REFERENCE WRAPPER CLASSES ******/
/******************************************************************************/

Names begin with a prefix which is added to the mnemonic (descriptive) part using underscore. Prefixes consist of two letters: the first letter defines the scope and the lifetime of the variable and the second letter defines its data type.


THE FIRST LETTER - SCOPE AND LIFETIME:

THE SECOND LETTER - DATA TYPE:

So, the convention doesn't distinguish between variables of primitive and reference types. Speaking about arguments of methods, it's more important to show difference between INPUT and OUTPUT arguments (i.e. the direction in which data is moved logically), so if a variable is used as an actual parameter (i.e. is passed to a method) in the OUTPUT mode then add a comment "/* OUT */" to the method call, for example:

THIS.getNumbers(li_someInputData, li_firstNumber /* OUT */, li_secondNumber /* OUT */);

We don't change the names of such variables because their passing for OUTPUT is not interesting in the rest of the program (later they can be even passed to other method as INPUT arguments!), but we change names of formal OUTPUT arguments (i.e. how they are named inside the method), and the rule is simple: add "Out" to the end of their names. So, inside of getNumbers() its OUTPUT arguments would be named ai_firstNumberOut and ai_secondNumberOut. Don't add "Out" to arguments, physically passed by reference (like all classes are) if logically they are INPUT arguments!


Examples:


Don't confuse letters which have different meaning when appear in prefixes in the first and in the second position:


Letter: Meaning in 1st position: Meaning in 2nd position:
l local long / Long
i instance integer / Integer
s static String


/*************************/
/****** GUI OBJECTS ******/
/*************************/

Prefixes of GUI objects (controls) consist of 3 letters.


Swing:


SWT (Standard Widget Toolkit):


/* UNDER CONSTRUCTION */


That is how the prefixes are used in names of GUI objects themselves: btn_cancel, txt_lastName, win_editCustomer. But these prefixes are used also in names of variables pointing to GUI objects; in this case in the name beginnig we add a one-letter prefix describing the pointer's scope. So, we use two prefixes divided by underscore:

/***********************/
/****** CONSTANTS ******/
/***********************/

Mnemonic part of constants is written in upper case; words are divided by underscore. Naming convention prefixes are always written in lower case: ls_MIN_AGE_TO_BUY_BEER.


/************************************************************************/
/****** CLASSES (NOT GUI CONTROLS OR WRAPPERS FOR PRIMITIVE TYPES) ******/
/************************************************************************/

Classes, which are not GUI controls or wrappers for primitive types (their definitions, NOT variables which point to them!) don't have any naming convention prefixes and begin with an upper-case letter, for example: PermanentEmployee.


As you have seen in the examples above, prefixes of references to objects (which are not GUI controls or wrappers for primitive types) have only one letter - it defines the variable's scope/lifetime. We don't add the second letter in this situation because there is unlimited number of possible classes (data types), so there is no way to invent data type indicators for all them. Examples:

There is one very important exception from the previous rule: variable pointing to an object of class String is marked with "s" - it has its own letter - like a primitive type! So, ls_firstName means "local String variable" (not l_firstName as it would be in case of pointers to other not-wrapper objects!). This exception is made because the class String hase extremely wide use (in many programming languages it's a built-in primitive type).


/************************/
/****** INTERFACES ******/
/************************/

Interfaces (their definitions, NOT variables of interface types!) don't have any naming convention prefixes, added with underscore, but have a capital letter "I" which is added to the mnemonic part without underscore. Mnemonic parts themselves begin with an upper-case letter, so TWO initial letters in upper case (first of which is "I") are a sign of an interface. For example: IEatable, IOrderStatusConstants. Pay attention that there can be class names beginning with I (like InternetConnection), but the second lower-case letter (or a not-letter character) testify that it's a class, not an interface (so, be careful naming your classes which begin with "I": IpAddress, not IPAddress!).


Prefixes of variables of interface types consist of only one letter (scope/lifetime). The name part after the underscore complies with the interface naming rules ("I" + a capital letter). Examples:

/************************/
/****** EXCEPTIONS ******/
/************************/

Classes, inherited from the class Exception directly of indirectly (their definitions, NOT variables which point to them!) don't have any naming convention prefixes, added with underscore, but have a capital letter "E" which is added to the mnemonic part without underscore. Mnemonic parts themselves begin with an upper-case letter, so TWO initial letters in upper case (first of which is "E") are a sign of an exception. For example: ENoFuelInPlane, EEmptyParmsPassed. Pay attention that there can be not-exception class names beginning with E (like EuropeanRestaurant), but the second lower-case letter (or a not-letter character) testify that it's an exception (so, be careful naming your not-exception classes which begin with "E": Ecommerce, not ECommerce!).


Prefixes of variables which point to object of exception types consist of only one letter (scope/lifetime) - as any pointer to a not-wrapper class. The name part after the underscore complies with the exception naming rules ("E" + a capital letter), for example: l_ENoFuelInPlane - local pointer to an object of ENoFuelInPlane class.


/************************************/
/****** ARRAYS AND COLLECTIONS ******/
/************************************/

Names of arrays must have "Array" in the end, names of collection must have the collection name (like "ArrayList", "LinkedList" or "HashSet") in the end. Their prefix always has the first letter (scope); second letter is added only if the array or collection is declared to keep pointers to a specific type (primitive or wrapper for arrays, and only wrapper for collections). For example:


/*********************/
/****** METHODS ******/
/*********************/

Methods don't have any naming convention prefixes (parentheses with zero or more arguments are enough indication that we see a method) and begin with a lower-case letter.






Any idea? Please email me!