Following on from my discussion on some common class name prefixes and suffixes I wanted to discuss some method name conventions. Again, this all relates to Java - other languages are entrely different.
Common method name patterns
For better or worse, Java is a pattern-based language. By that, I mean that a lot of what is considered to be best practice in Java works is by following standard patterns. I don't just mean the Gang of Four book here. I mean the myriad of other patterns. These are often "visual patterns" as much as code patterns. For exmaple, by laying our code out in a certain consistent way, we aid the developer that follows us.
To be clear, I'm not saying that a pattern-based/naming approach is better than new language features. Its just that in Java that is the best we have to deal with many issues. (Cut and Paste is a way of life in Java, and its not always bad.)
One of these patterns is method names. This approach in Java is heavily influenced by two things - the size and dominance of the naming choices in the JDK, and the design of Java-Beans. Back in 2006, I proposed some additional variants for immutable classes, popularized by Joda-Time. This list includes both, plus some new ones I've added from ThreeTen/JSR-310. It isn't intended to list every possible method name convention, just some of the more popular, and those with specific semantics.
|get||Gets something from an object, such as |
|is||Checks if something is true or false about an object or a property of an object. Example |
|check||Checks if something is true, throwing an exception if it is not true. Example |
|contains||Checks if a collection contains the queried object, such as |
|remove||Removes an element from a collection, such as |
|clear||Clears the object, typically but not necessarily a collection, so that it is "empty".|
|put||Mutable putter. This mutates the target object replacing or adding some form of key-value pair. Examples are |
|set||Mutable setter. This mutates the target object setting a property, such as |
|with||Immutable "setter". Returns a copy of the original with one or more properties changed, such as |
|to||Converts this object to an independent object, generally of another type. This generally takes no arguments, but might if it is appropriate.|
|as||Converts this object to another object where changes to the original are exposed in the result, such as |
|build||Builds another object based on either the specified arguments, the state of the target object, or both.|
|add/subtract||Adds/subracts a value to the quantity. This mutates the target quantity (a number, date, time, distance...) adding/subracting the "foo" property. This name is also separately used for adding elements to a collection.|
|plus/minus||Immutable version of add/subract for a quantity. Returns a copy of the original with the value added/subtracted. This name does not seem to work as well for adding elements to an immutable collection.|
|append||Sometimes used to by methods that add to the end of a list, such as in |
|reset||Resets the object back to a suitable initial state ready to be re-used.|
|past tense||Used on immutable classes to helpfully suggest to the caller that the method doesn't mutate the target, but must instead be assigned to another variable. Returns a copy of the target object with the method name applied. Examples are |
And here are some static method names:
|of||Static factory method. Typically used with immutable classes where constructors are private (permitting caching). This is used by |
|valueOf||Longer form of |
|from||Longer form of |
|parse||Static factory method that creates an instance of the class by parsing a string. This could just be another |
Are there any more common names that I've missed? Comments welcome...