Do you use verbs for method names?

Last updated by Tiago Araújo [SSW] about 2 months ago.See history

Code becomes easier to understand when names align closely with their meaning. Since classes represent things, they should use nouns. Methods, on the other hand, represent actions and should be named using verbs. A method named Person, for example, could easily confuse your team by suggesting an entity rather than an action.

What is a verb?

A verb is a word that describes an action or process—something that’s done. Examples include walk, run, think, listen, and breathe, as well as process, calculate, send, and save.

Naming methods as verbs

While your method names shouldn’t all be single verbs (since that’s often too vague), they should be verb-based, using a verb as the foundation of their meaning.

Using Ship as a method to initiate shipping orders to customers.

Bad example - While 'ship' is a verb, it’s also a noun, making it ambiguous. Plus, it's not very descriptive!

Using SendCustomerOrder

Good example - A method that is named as a verb clearly tells you that it does something (sends), and what that thing is (a customer order)

Using Administration as a method name for nurses to document medications that have been administered to patients.

Bad example - It’s a noun, and it’s ambiguous. 'Administration' has multiple meanings, and the method name doesn’t make it clear what this action involves

Using AdministerMedication makes the action clear

Good example - Method name describes the action

We open source.Loving SSW Rules? Star us on GitHub. Star
Stand by... we're migrating this site to TinaCMS