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 bean.getBar() . This is also used with a key to lookup a list by index or a map by key, such as list.get(index) or map.get(key) . |
is | Checks if something is true or false about an object or a property of an object. Example foo.isValid() . |
check | Checks if something is true, throwing an exception if it is not true. Example foo.checkValid() . |
contains | Checks if a collection contains the queried object, such as coll.contains(bar) . This can be used on classes that wrap, or otherwise act as, a collection. |
remove | Removes an element from a collection, such as coll.remove(bar) . This can be used on classes that wrap, or otherwise act as, a collection. |
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 map.put(key,bar) and bean.putFoo(key,bar) |
set | Mutable setter. This mutates the target object setting a property, such as bean.getBar(bar) . |
with | Immutable "setter". Returns a copy of the original with one or more properties changed, such as result = original.withBar(bar) . |
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 Arrays.asList() . |
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 StringBuilder . |
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 immutable.normalized() , immutable.multipliedBy(bar) , immutable.dividedBy(bar) and immutable.negated() |
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 EnumSet and ThreeTen/JSR-310. |
valueOf | Longer form of of used by the JDK. |
from | Longer form of of . JSR-310 uses 'from' when performing a "loose" conversion between types, ie. one that has a reasonable chance of failure. By contrast 'of' is used when the conversion is almost certain to succeed. |
parse | Static factory method that creates an instance of the class by parsing a string. This could just be another of method, but I think the semantics are clearer with a dedicated name. |
Are there any more common names that I've missed? Comments welcome...