Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

determining which verb to use for method names in Java

I understand that naming conventions are important for a number of reasons, most having to do with making your code more readable and easier to integrate into larger projects, etc. In Java, most conventions require that method names be in lowerCamelCase begin with a verb.

My question is: how do I choose the verb to begin the method name?

To make this question less vague, I'm often in the situation where my first choice for a method name is a noun describing the output. In these cases, I'm usually torn between appending generic verbs such as get, generate, calculate, etc in font of the noun to conform to the verb rule. Are there general guidelines for when to use which?

Here's an example. I have a method that takes double[] array and an int k and returns double[] newArray which is the length k moving average of array, i.e. newArray[i] = (array[i-k+1]+...+array[i])/k with some fudging to make newArray the same length as array. My inclination is to call this method movingAverage since that's what it returns, but that's out since it doesn't begin with a verb. Should I call this method getMovingAverage or generateMovingAverage or calculateMovingAverage or does it not really matter?

like image 843
PengOne Avatar asked Aug 22 '11 17:08

PengOne


People also ask

Should method names be verbs?

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.

What is the naming convention followed for methods in Java?

Java uses CamelCase as a practice for writing names of methods, variables, classes, packages, and constants.

Which of the below method name is valid as per Java naming convention?

CamelCase in Java naming conventions Java follows camel-case syntax for naming the class, interface, method, and variable. If the name is combined with two words, the second word will start with uppercase letter always such as actionPerformed(), firstName, ActionEvent, ActionListener, etc.


2 Answers

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.

like image 188
Evan Mulawski Avatar answered Sep 22 '22 08:09

Evan Mulawski


Do not forget to use this verbs "is, has or can" for boolean methods, such as: isOn(), isFull(), and so on.

like image 45
Ricardo Montesinos Avatar answered Sep 26 '22 08:09

Ricardo Montesinos