by Serguei A. Mokhov
Some parts borrowed from Merck Frosst's
R.I.S. Department Coding Conventions
by Jean Marois.
$Revision: 1.1 $
$Date: 2005/05/28 04:51:58 $
This section describes our proposed common naming and coding conventions,
which all of us will follow in order to achieve consistency and
uniform style in our projects.
[TOP]
This technique makes programming easier, because it gives you a simple mental machine for naming the things in your code. A guy, Charles Simonyi, invented that technique that solves an old and tiresome programming problem: "What am I going to name this variable?" He realized that there are two important aspects to think about when writing code: what a thing is, and what it means. To write good programs in strongly-typed languages, you really need to keep both of these things in mind. The "traditional" naming technique uses names that emphasize what a thing means, requiring you to memorize its declaration to remember what that thing is. While good in its intention of making code "readable", this traditional kind of naming often results in two things:
Hungarian names have two parts. The prefix contains type information specifying what this thing is, while the suffix contains descriptive information specifying what it means. The prefix is composed of standard elements that are the same for any program you write in a given language. The suffix is composed of English words that tend to be application-specific.
The prefix elements used here are not the only ones possible, but they
have some particularly nice properties. First, they are highly mnemonic,
so it's easy to remember what they mean. Second, they all consist of a
single or a few letters. This becomes important when multiple prefix elements are
composed in the same name. A prefix can show a lot of information in a
few characters, without possibility of confusion about how those
characters are associated.
[TOP]
This proposed naming scheme is supposed to be used by everyone programming in Java to maintain consistency among the code base for the projects where the Java programming language is used.
getStudentName();
- is a getter, where strStudentName
is a data member of a given class;crashWindows();
die();
setMoney(Money poMoney);
Prefix | Description |
---|---|
m_ | The prefix denotes a member variable of a class. |
p_ | This prefix shows that the variable is a parameter in the function's signature. |
l_ | This prefix is used for the local scope variables (e.g. inside a function) |
g_ | This prefix describes variables belonging to the global scope. |
this.
".
piID
in getProductInfo(int piID)
.
Letter | Type |
---|---|
i | int |
l | long |
f | float ; floating point, single precision |
d | double ; floating point with double precision |
c | char |
b | boolean |
t | byte |
After all type identifications some (logical) wordings may follow to distinguish variables of the same type in the same scope.
Examples:
Variable Name | Meaning |
---|---|
iIndex |
This is a local variable of an int type, named Index. |
oGun |
This is a local variable of type of an object of class Gun. |
this.acMyCharArray |
This is an array of characters named MyCharArray, which is a member variable. |
poGunRevolver |
This is a parameter variable passed to a function of
type of object Gun, named Revolver ( void myFunc(Gun poGunRevolver) {} )
|
strError |
This is a character string variable named Error. |
Constants may follow the same rules as the variables as far as prefixes concerned (optionally),
and usually are CAPITALIZED. Every other word in the case of a multiword constant
(and with capitalization) must be separated with underscores "_".
E.g.: public static final int DEFAULT_NUMBER_OF_WORKERS = 13;
Each class has to have the following structure:
/**
* javadoc comment goes here to describe the class
* @author Serguei A. Mokhov
* @since 1.0
* @version $Revision: 1.1 $ $Date: 2005/05/28 04:51:58 $
*/
class MyClass [ extends WhateverClass ] [ implements WhateverInterface ]
{
/*
* ------------
* Data Members
* ------------
*/
/**
* Just a Name
*/
private String strName;
/*
* ---------------
* Object Lifetime
* ---------------
*/
/**
* Constructor
*/
public MyClass()
{
}
/**
* Performs some cleanup, e.g. closing files/releasing resources
*/
private void cleanUp()
{
}
/*
* -----------------
* Getters / Setters
* -----------------
*/
/**
* Retrieves name
* @return Name of the object, String
*/
public final String getName()
{
return this.strName;
}
/**
* Sets the name of the object. Name must not be empty.
* @param pstrName Name of the object
* @exception RuntimeException thrown if the name is an empty string
*/
public final void setName(final String pstrName) throws RuntimeException
{
if(pstrName == null || pstrName.equals(""))
throw new RuntimeException("Name cannot be empty.");
this.strName = pstrName;
}
/*
* ----------------
* Public Interface
* ----------------
*/
/**
* Computes something of extreme importance
*/
public void computeSomething()
{
}
}
Opening and closing scope (curly) brackets should be at the same indentation interval, one under another:
void blah() { for(;;) { if() { ... } else { ... } } }
for()
or while()
loops for example, or
if()
statements.
Comments should be meaningful and precede the line they are referring to; they should not be on the same line.
Putting all together:
/** * Class Gun that fires. * Provides ability to set index and fire. * @author Gun Shop * @version $Revision: 1.1 $ * @since 1.2.3 */ class Gun { /* * ----------------- * Getters / Setters * ----------------- */ /** * Last Error Occurred. */ private String strError; /** * Serial Index of the Weapon (if such exists :)). */ private int iIndex; /* * --------------- * Object Lifetime * --------------- */ /** * Constructor. * @param piIndex Serial number of the gun */ public Gun(final int piIndex) { setIndex(piIndex); } /* * ----------------- * Getters / Setters * ----------------- */ /** * Retrieves the serial index of the weapon. * @return Gun's serial index */ public final int getIndex() { return this.iIndex; } /** * Sets the serial index of the weapon. * @param piIndex Gun's serial index */ public final void setIndex(final int piIndex) { this.iIndex = piIndex; } /** * Retrieves the error message of the last error occurred. * @return Last error message string */ public final String getError() { return this.strError; } /* * ---------------- * Public Interface * ---------------- */ /** * Fires the gun. */ public void fire() { } } /** * Main startup module. */ public class Main { /** * Important array of integers shared among all class * instances. */ private static int[] saiMyIntArray = {1, 2, 3, 4, 5, 6, 7}; /** * argv here left as-is for historical reasons, but may * be changed to pastrCommandLineArgs or whatever plausible * replacement is. * @param argv command line arguments vector */ public static void main(String[] argv) { new Main().use(new Gun()); } public void use(Gun poGun) { //... poGun.fire(); //... } public void errorHandler(int piErrorID) { //... } }
this.
" prefix can be used. However, if we
omit the prefix, it will look like as if we are using local variables
inside our methods.
Therefore, we will use the "s" prefix for statics as we do use "p" for
parameter variables.
Examples: private static sstrFormName = "";
,
public static int siNextTID = 1;
ICommunicationProcedure
, so we will use
that for interfaces.
This document was created by Serguei Mokhov on June 3, 2003
Last updated: $Date: 2005/05/28 04:51:58 $