Naming Conventions

Description

This note describes different naming conventions when developing code. Basically normal Java naming conventions are used, in order that the code should be easy to read by other developers.

Accessor Method Names

General

Methods for getting or setting the value of an instance variable should normally have names starting with "get" or "set", respectively, except that prefix "is" can be used for getter methods with Boolean or boolean return type.

In Classes used by TableFactory

If TableFactory is used to display the database contents for a class, the created table will by default have one column for each method with name starting with "get" or "is", and the method name without this prefix will be used as the string key for the column. The column key is used in TableFactory to identify the column, e.g. for specifying a special column order, or that a column should be hidden. The column key is also used as key in localization dictionary files to find the header name for the column (if no entry exists, the key is used as name). Adding or changing the name of a getter method in a class used by TableFactory, may therefore require changes in the TableFactory class itself, and all localization dictionary files. For more details, see TableFactory Use: Getter Method Name.

Accessor Methods for Variables Representing Quantities in Units

Methods retrieving values expressed in specific units should have names with a suffix denoting the unit used, e.g. if getSizeX() and getWeight() are expressed in centimeters and micrograms, respectively, they should be renamed getSizeXInCentiMeters() and getWeightInMicroGrams(). This way there will be no question about what unit the data is in. Setter methods should be named analogously, e.g. setSizeXInCentiMeters(Float newSizeX) and setWeightInMicroGrams(Integer newWeight).

Conventions for Unit Suffixes in Method Names

A number of spelling conventions for unit suffixes have been set up, in order to have a simple set of common rules for naming the methods. This minimizes the risk of entering the wrong string key in dictionary files, even though the expression for the unit may differ from the one conventional in print.

  1. The unit suffix is written without abbreviations, e.g. getSizeXInCentiMeters() instead of getSizeXInCm().
  2. The unit suffix is written in plural, e.g. getSizeXInCentiMeters() instead of getSizeXInCentiMeter().
  3. The forms "meters", "liters", and "grams" are preferred over "metres", "litres", and "grammes", respectively.
  4. When the unit name contains an exponent prefix, such as "micro", "centi", or "milli", both the exponent prefix and base unit name are capitalized, e.g. getSizeXInCentiMeters() instead of getSizeXInCentimeters().