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.
Wouldn't it be even better the seperate the two out into namespaces and/or classes?
So have something like:
Command namespace
User class
Create static method
Then all this just drops out without having to worry about it - everything groups itself naturally. Why create conventions when you can do it better with the language?
I'd say the main point is: be consistent.
Your approach may be the best, but even for a non-native English-speaker a class like UserCreateCommand sounds ugly. You can achieve almost the same level of structure if you go with the more natural verb-noun order. What you have to do then is to define common set of verbs for your actions (edit,create,find...whatever) and be consistent there.
In our project we did it wrong from the beginning but after a large refactoring we reached the point where we're almost there.
Forth?
same thing for UI controller bean classes ;-)
Understanding that concept is confusing considering I might or might not know what you are or aren't talking about.I just don't understand why everything has to be in order. For example why do we have parents ! why can't we just produce asexually. That way stuff like attempted murder can happen except it would only create more of them.
That would be kewl to kill people and like more of it appears. and if you tried to kill me it would only create more of them. Which would also be really cool.