Java Reference
In-Depth Information
Table 9.2 Here are some coding conventions for naming. We've included an example of the
intended usage, with the entities observing the rule in bold.
Naming rule
Usage
For fields and attributes, use descriptive nouns with
restrictive adjectives where appropriate.
StateTax
shippingAddress
Use a descriptive test prefix for Boolean attributes ,
like is , contains or has .
if(anItem. isTaxable ) {
// do something
}
if (collection. hasTaxableItems ) {
// do something
}
Name Boolean accessors with a descriptive test, like
is , has , or contains . get and set are not
required.
person.isPersistent()
while(table. hasMoreRows ()) {
// do something
}
Make collection names plural for clarification.
shoppingCart.items
For methods, use descriptive verbs with restrictive
nouns indicating targets where appropriate.
command.execute()
computeInterest()
Name accessors with get and set , followed by the
field names.
getCustomer()
setCustomerName()
Exception: Single letters are acceptable for loop
counters, for the economy of space, as long as read-
ability remains clear.
for (i=0; i<10, i++) {
// do something
}
method names are not. Constants are capitalized, with words separated
by underscores. Packages are all lowercase:
Customer // class
customerFirstName // attribute
java.lang // package
CUSTOMER COLUMN // constant
Programmers don't always apply capitalization rules consistently. Acronyms
and abbreviations are a particular source of confusion. For capitalization of
abbreviations, which aren't the same as acronyms, the standard libraries are
slightly inconsistent but generally go with full capitalization. Here are some
examples:
URL
IOException
SQLException
SAXParser
HTMLEditorKit
Search WWH ::




Custom Search