determining which verb to use for method names in Java

I usually ask myself:

What is this method doing?

The answer dictates what the method should be called. It is completely independent of the programmer, of course.

Note: If you can’t succinctly describe what the method is doing, it’s probably doing too much and should be split up.

Choosing your method’s verb:

  • Performing calculation(s): calculate
  • Retrieving data: get or retrieve
  • Mutating data: set or change
  • Deleting data: delete or remove
  • Converting: convert
  • Initiating an action: start or initiate
  • Stopping an action: stop or cancel

Now, not all methods begin with a verb; but they really don’t need to. If you read:

... myString.length();

or

... myArray.size();

you know exactly what is going on – no verb required. This is true for many class methods higher up in the Java hierarchy; Collections, Math, etc. As long as the name accurately communicates what the method does, it’s fine.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)