Geomajas
Community Documentation
2.1. Class, method and variable names
Rules
-
Use meaningful names. Especially class and method names should explain their purpose.
-
For class, method and (non-static) variable names, use camelCase to separate the words, not underscores. For abbreviations, capitalize he first letter, lower case for the others.
-
Class names start with a capital, for example "CommandDispatcher".
-
Method and (non-static) variable names start lower case, for example "getEmptyCommandResponse".
-
All static variables should have capitalized names with words separated by underscores.
-
Package names are all lower case and should be singular.
-
Use get/set/isXxx.
-
Abbreviations and acronyms should not be uppercase when used as name (for example, use "exportHtml()").
-
All names should be written in English.
-
The terms get/set must be used where an attribute is accessed directly.
-
"is" prefix should be used for boolean variables and methods. In some cases, when this is more readable, "has", "can" or "should" can also be used as prefix.
-
Complement names must be used for complement entities. These include get/set, add/remove, create/destroy, start/stop, insert/delete, increment/decrement, old/new, begin/end, first/last, up/down, min/max, next/previous, old/new, open/close, show/hide, suspend/resume, etc.
-
Exception classes should be suffixed with Exception.
Recommendations
-
Usually class names are nouns and method names are verbs.
-
Generic variables should have the same name as their type.
-
Variables with a large scope should have long names, variables with a small scope can have short names. Scratch variables used for temporary storage or indices are best kept short. A programmer reading such variables should be able to assume that its value is not used outside a few lines of code. Common scratch variables for integers are i, j, k, m, n and for characters c and d.
-
The name of the object is implicit, and should be avoided in a method name. For example, use "line.getLength()" instead of "line.getLineLength()". The latter might seem natural in the class declaration, but proves superfluous in use, as shown in the example.
-
The term compute can be used in methods where something is computed.
-
The term find can be used in methods where something is looked up.
-
The term initialize can be used where an object or a concept is established.
-
Plural form should be used on names representing a collection of objects.
-
Negated boolean variable names must be avoided.
-
Default interface implementations can be prefixed by Default. However, if it is not expected that there will even be another implementation, it can be a lot more natural to suffix with "Impl" instead.
-
Singleton classes should return their sole instance through method getInstance, should have a private constructor and be declared final.
-
Functions (methods returning an object) should be named after what they return and procedures (void methods) after what they do.
-
Data transfer objects sometimes exist in two flavors, one which contains the Geomajas geometry dto's and one which contains JTS geometry objects. In that case, the variant with the geometry dto's should use the natural name, and the variant with JTS geometry objects should have a class name which has the "JG" suffix (JG stands for Jts Geometry).