Java Reference
In-Depth Information
In the workplace, management will usually specify which particular style must be used.
For this topic, and for the SCJD assignment, we recommend you use the Sun Code Conventions
for the Java Programming Language, which you can download at http://java.sun.com/
docs/codeconv/ . Sun has specified 11 areas where they believe coding guidelines are needed,
and these can be grouped into the following major categories: naming conventions, file lay-
out, source code format, and comment format. We introduce each of these categories next.
Naming Conventions
The Sun Code Conventions specify different naming conventions for packages, classes and
interfaces, methods, variables, and constants.
For all of the naming conventions, you should try to avoid using abbreviations, except
where the abbreviation is more commonly recognized than the complete word. At the same
time, you should try not to make your names too long, or they will quickly become tedious to
read and write. Consider the variable names shown in Table 2-2.
Table 2-2. Variable Naming Examples
Contents
Good Variable Name
Poor Variable Name
accountBalance
usersCurrentAccountBalance (too long)
ab (not a common abbreviation; doesn't
mean much)
The balance of an account
HTML editor class
HtmlEditor
HyperTextMarkupLanguageEditor
(“HTML” is a common abbreviation, so
using it will enhance the readability of
this class name)
Package Naming Conventions
Package names start with your fully qualified domain name, written in lowercase and in
reverse. So if you worked for a company with the domain name example.com , then your pack-
age names should start with the same name in reverse ( com.example ).
From that point on, you would follow your company's naming conventions. A sample
naming convention might be to use the project name, followed by a conceptual grouping of
classes. For instance, as we are working on the SCJD project, which contains a GUI client, we
could have a combined package name of com.sun.edu.scjd.gui (for simplicity though, we
have used the base package name of sampleproject throughout our project, and the GUI code
is therefore in package sampleproject.gui ).
Tip In the real assignment, Sun will typically specify a package name for at least one class. Therefore,
you do not need to be concerned that you do not have an existing domain name that you can use as your
base package name.
Search WWH ::




Custom Search