Naming Conventions


You should have noticed a naming pattern in the Java code you have written so far. Most of the elements in Java that you have already learned aboutfields, formal parameters, methods, and local variablesare named in a similar way. This convention is sometimes referred to as camel case.[12] When following the camel case naming pattern, you comprise a name, or identifier, of words concatenated directly together. You begin each word in the identifier, except for the first word, with a capital letter.

[12] Imagine that the letters represent the view of a camel from the side. The upper letters are the humps. For an interesting discussion of the term, see http://c2.com/cgi/wiki?CamelCase. You may hear other names for camel case, such as "mixed case."

You should name fields using nouns. The name should describe what the field is used for or what it represents, not how it is implemented. Prefixes and suffixes that belie the type of the field are unnecessary and should be avoided. Examples of field names to avoid are firstNameString, TRim, and sDescription.

Examples of good field names include firstName, trimmer, description, name, mediaController, and lastOrderPlaced.

Methods are generally either actions or queries: Either you are sending a message to tell an object to do something or you are asking an object for some information. You should use verbs to name action methods. You should also use verbs for query method names. The usual Java convention is to prefix the name of the attribute being retrieved by the word get, as in getNumberOfStudents and getStudent. I will discuss some exceptions to this rule later.

Examples of good method names include sell, cancelOrder, and isDoorClosed.

Name classes using upper camel casecamel case where the first letter of the identifier is uppercased.[13] You should almost always use nouns for class namesobjects are abstractions of things. Do not use plural words for class names. A class is used to create a single object at a time; for example, a Student class can create a Student object. When creating collections of objects, as you will learn to do later, continue to use a nonplural name: StudentDirectory instead of Students. From a StudentDirectory class, you can create a StudentDirectory object. From a Students class, you could create a Students object, but it sounds awkward and leads to similarly awkward code phrases.

[13] Camel case is thus sometimes called lower camel case to distinguish it from upper camel case.

Examples of good class names include Rectangle, CompactDisc, LaundryList, and HourlyPayStrategy.

In terms of good object-oriented design, you will find that design impacts the ability to name things. A welldesigned class does one thing of importance and only that one thing.[14] Classes generally should not do a multitude of things. For example, if you have a class that cuts paychecks, prints a report of all the checks, and calculates chargebacks to various departments, it would be hard to come up with a name that succinctly describes the class. Instead, break the class up into three separate classes: CheckWriter, PayrollSummaryReport, and ChargebackCalculator.

[14] A guideline known as the Single-Responsibility Principle [Martin2003].

You can use numbers in an identifier, but you cannot use a number as its first character. Avoid special charactersthe Java compiler disallows many of them. Avoid underscores (_), particularly as separators between words. There are exceptions: As mentioned earlier, some developers prefer to prefix their fields with underscores. Also, class constants (to be introduced later) usually include underscores.

Avoid abbreviations. Clarity in software is highly valued. Take the time to type the few extra characters. Doing so is far cheaper than increasing the time it takes for a future reader to comprehend your code. Replace names such as cust and num with the more expressive custard and numerology.[15] Modern IDEs provide support for quickly renaming things in your code and for auto-completing things you are typing, so you have no excuse to use cryptic names. Abbreviations are acceptable if you use the abbreviation in common speech, such as id or tvAdapter.

[15] I'll bet you thought they stood for customer and number. See how easy it is to be led astray?

Remember that Java is case sensitive. This means that stuDent represents a different name than student. It is bad form to introduce this sort of confusion in naming, however.

These naming conventions indicate that there are commonly accepted coding standards for things that aren't necessarily controlled by the compiler. The compiler will allow you to use hideous names that will irritate your fellow developers. Most shops wisely adopt a common standard to be followed by all developers. The Java community has also come to a higher level of agreement on such standards than, say, the C++ community. Sun's coding conventions for Java, located at http://java.sun.com/docs/codeconv, is a good, albeit incomplete and outdated, starting point for creating your own coding standards. Other style/standards books exist, including Essential Java Style [16] and Elements of Java Style.[17]

[16] [Langr2000].

[17] [Vermeulen2000].



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net