Catalog of Method Types

Home - About » Computer Science - Programming - Conventions
Computer Science
Research, Industry Work,
Programming
Community Service
Hillside Group, CHOOSE,
Stanford GSA
The Serious Side
Business School,
Learning Chinese
Humorous Takes
Switzerland, United States,
Software, Fun Photos
Travel Stories
Europe, United States, Asia
  
Living Places
Berlin (+ Gallery), Zürich
Boston, S.F. + Bay Area

(Some of this material has been taken from a Java Report paper.)

We distinguish three main categories of method types (query methods, mutation methods, and helper methods) from specific method types of these categories.

If you want to add method types to this list, let me know!

Query methods

A query method (aka accessing method) is a method that returns some information about the object being queried. It does not change the object's state. There are four main query method types: get method, boolean query method, comparison method, and conversion method.

Get method

Definition: A get method is a query method that returns a (logical) field of the object.
Also known as: Getter.
JDK example: Class Object::getClass(), Object Enumeration::nextElement().
Name example: String Name::component(int), NameEnumeration Name::names().
Prefixes: (If any:) get.
Naming: After the prefix (if any), the name of the field being queried follows.

Boolean query method

Definition: A boolean query method is a query method that returns a boolean value.
Also known as: -
JDK example: boolean Object::equals().
Name example: boolean Name::isEmpty().
Prefixes: is, has, may, can, ...
Naming: After the prefix, the aspect being queried follows.

Comparison method

Definition: A comparison method is a query method that compares two objects and returns a value that tells you which object is "greater" or "lesser" than the other one according to an ordering criterion.
Also known as: Comparing method.
JDK example: boolean Object::equals(Object), int Comparable::compareTo(Object).
Name example: boolean Name::isEqual(Object).
Prefixes: If also a boolean query method, see boolean query method.
Naming: If also a boolean query method, see boolean query method.

Conversion method

Definition: A conversion method is a query method that returns an object that represents the object being queried using some other (typically simpler) object.
Also known as: Converter method, interpretation method.
JDK example: String Object::toString(), int Integer::intValue().
Name example: String Name::asString(), String Name::asStringWith(char).
Prefixes: as, to.
Naming: After the prefix, typically the class name being converted to follows.

Mutation methods

A mutation method is a method that changes the object's state (mutates it). Typically, it does not return a value to the client. There are three main mutation method types: set method, command method, and initialization method.

Set method

Definition: A set method is a mutation method that sets a (logical) field of an object to some value.
Also known as: Setter.
JDK example: void Thread::setPriority(int), void Vector::addElement(Object).
Name example: void Name::component(String).
Prefixes: (If any:) set.
Naming: After the prefix (if any), the field being changed follows.

Command method

Definition: A command method is a method that executes a complex change to the object's state, typically involving several fields and affecting further related objects.
Also known as: -
JDK example: void Object::notify(), void JComponent::repaint().
Name example: void Name::insert(int i, String c), void Name::remove(int i).
Prefixes: (If any:) handle, execute, make.
Naming: -

Initialization method

Definition: An initialization method is a mutation method that sets some or all fields of an object to an initial value.
Also known as: -
JDK example: void LookAndFeel::initialize().
Name example: void StringName::initialize(), void VectorName::initialize().
Prefixes: init, initialize.
Naming: If prefixed with init, typically the name of the object part being initialized follows.

Helper methods

A helper method (aka utility method) is a method that performs some support task for the calling object. A helper method does not change the object's state, but only performs operations on the method arguments. Frequently, a helper method is a static method and provided by some helper (utility) class.

Factory method

Definition: A factory method is a helper method that creates an object and returns it to the client.
Also known as: Object creation method.
JDK example: String String::valueOf(int), String String::valueOf(double).
Name example: NameEnumeration Name::names(), Name StringName::newName(String).
Prefixes: (If any:) new, create, make, build.
Naming: After the prefix, the product name follows.

Assertion method

Definition: An assertion method is a helper method that checks whether a certain condition holds. If the condition holds, it returns silently. If it does not hold, an appropriate exception is thrown.
Also known as: -
JDK example: void AccessControlContext::checkPermission(Permission) throws AccessControlException
Name example: void Name::assertIsValidIndex(int) throws InvalidIndexException.
Prefixes: assert, check, test.
Naming: After the prefix, the condition being checked follows.
Copyright (©) 2007 Dirk Riehle. Some rights reserved. (Creative Commons License BY-NC-SA.) Original Web Location: http://www.riehle.org