Mini-Pattern: Noun-Verb

by Charles Miller on October 22, 2003

Name: Noun-Verb

Context:

Your application1 has a significant number of command or action classes, for which a consistent naming scheme should be chosen.

Forces:

  • Command classes generally perform an action (verb) on a thing (noun)
  • The natural ordering in the English language is verb-noun. i.e. "Create user" or "Change address". By default, English-speaking programmers will default to this ordering
  • Most development environments sort class names alphabetically
  • It is easier to locate classes when they grouped near related classes.
  • The relation between two actions with the same noun: "Create User" and "Update User" is far more significant than that between those with the same verb: "Update User" and "Update Shopping Cart"
  • Given a use-case, it is much easier to find classes grouped by noun rather than verb. For example, if the use case is "changing a user's details", it is easier to scan through all the commands that affect a User than it is to try to work out if the applicable verb is "update", "change", "alter" or "modify".
  • Because command classes are often referred to outside the main body of the code (for example in configuration files), this can complicate refactoring the class names.

Therefore:

Early on, require that command or action classes are named by their noun first. then the verb: i.e. "UserCreateCommand", or "UserUpdateInformationAction".

This advice can also be applied to related artifacts such as JSPs.

Examples of Use:

Every project I have been personally involved with where command-like objects (Servlets, the Struts framework) have been involved.

Almost inevitably, English speakers default to the common verb-noun ordering, which causes no problems when the project is young. Later, when the action classes fill an entire page, a significant effort is required in order to refactor to the noun-verb ordering, but that refactoring is almost always worthwhile.

And you always wish you'd thought of it earlier.

1 This may also apply to other languages, but I encounter it most often, and in fact almost inevitably, in Java webapps.

Previously: Return to the Planet of the Exceptions

Next: If Music be the Food of Love...