JAVA Naming Conventions
JAVA Naming Conventions
JAVA Naming Conventions
2
Files
} Java source files have the following ordering:
3
Comments
All source files should begin with a c-style comment that lists the class name,
version information, date, and copyright notice:
/*
*
Classname
*
*
Version
information
*
*
Date
*
*
Copyright
notice
*
*/
The first non-comment line of most Java source files is a package statement.
After that, import statements can follow. For example:
package
java.awt;
import
java.awt.peer.CanvasPeer;
Indentation
} Avoid lines longer than 80 characters, since they're not
handled well by many terminals and tools.
} Wrapping Lines
} When an expression will not fit on a single line, break it
according to these general principles:
} Break after a comma.
} Break before an operator.
} Prefer higher-level breaks to lower-level breaks.
} Align the new line with the beginning of the expression at the
same level on the previous line.
} If the above rules lead to confusing code or to code that's
squished up against the right margin, just indent 8 spaces
instead.
5
Indentation. Examples
6
Indentation. Examples (II)
7
Indentation. Examples (III)
8
Declarations
} One declaration per line is recommended
since it encourages commenting. In other
words,
int level; // indentation level
int size; // size of table
} is preferred over
int level, size;
} Do not put different types on the same line.
Example:
int foo, foo array[]; //WRONG!
9
Declarations
} Try to initialize local variables where they're declared.
The only reason not to initialize a variable where it's
declared is if the initial value depends on some
computation occurring first. Example:
int
count;
...
myMethod()
{
if
(condition)
{
int
count
=
0;
//
AVOID!
... }
... }
10
Declarations
} Classes and interfaces
} No space between a method name and the parenthesis
"(" starting its parameter list
} Open brace "{" appears at the end of the same line as the
declaration statement
11
Naming conventions
12
Naming conventions
Classes
Class names should be nouns, in mixed case class Raster;
with the first letter of each internal word class ImageSprite;
capitalized. Try to keep your class names
simple and descriptive. Use whole words-avoid
acronyms and abbreviations (unless the
abbreviation is much more widely used than
the long form, such as URL or HTML).
Interfaces
Interface names should be capitalized like interface RasterDelegate;
class names. interface Storing;
Methods
Methods should be verbs, in mixed case with the run();
first letter lowercase, with the first letter of each runFast();
internal word capitalized. getBackground();
13
Naming conventions
Variables
Except for variables, all instance, class, and class
constants are in mixed case with a lowercase first int i;
letter. Internal words start with capital letters. char c;
Variable names should not start with underscore _ float myWidth;
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.
14
Naming conventions
Constants
The names of variables declared static final int MIN_WIDTH = 4;
class constants and of ANSI static final int MAX_WIDTH = 999;
constants should be all uppercase static final int GET_THE_CPU = 1;
with words separated by
underscores ("_"). (ANSI
constants should be avoided, for
ease of debugging.)
15