Do method names always have to be verb?

Viewed 10680

I am new to java programming, and I am currently working on a command reading program (basically the user types in a command and my program evaluates what to do and does it). I have a separate class that contains all my commands, but they're stored as methods and aren't always using verbs as names. I understand that it is customary have methods stored as verbs. Am I using methods wrong and is there a better way to store my commands? Separate class for each command? Example of calling one of my methods:

else if (command[0].equals("math")) Commands.math();
4 Answers
  1. Must methods be verbs? No. As long as the compiler is concerned, it doesn't matter.

  2. Should they be verbs? As a convention that will make programs easier to understand: Clearly yes.

Variables represent objects and data, so you should normally use a noun. Methods represent actions, so you should normally use a verb. There are exceptions, of course but that is the normal rule. Ideally the name of a variable or method should be enough to identify it's function in the class.

Related