Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function naming conventions [closed]

I am writing a library, so, I want its functions to be named as clearly and cleverly as possible. Currently, I use the following principles:

  1. Self-explanatory names: a function getName() will tell the developer what it returns as well as setAddress(), isMale(), etc.
  2. Short: a function name must be as short as possible so that it's simple to type as well as easy to remember. A function getNumberOfPagesInTheBook() is not good, something like getBookPageCount() is better.
  3. Use of prefixes: I always use prefixes in the functions such as getName(), setName(), hasHair(), isBlond(), etc.

I'm interested in knowing if there's something I'm missing. Also, can you think of some other prefixes other than is, has, get and set?

like image 672
Tower Avatar asked Jan 02 '10 11:01

Tower


People also ask

Are function naming conventions?

Naming Convention for Functions So, similar to variables, the camel case approach is the recommended way to declare function names. In addition to that, you should use descriptive nouns and verbs as prefixes. For example, if we declare a function to retrieve a name, the function name should be getName.

Should function names always be verbs?

One of the more universal, yet simple rules is: Function names should be verbs if the function changes the state of the program, and nouns if they're used to return a certain value.


2 Answers

One of the more universal, yet simple rules is: Function names should be verbs if the function changes the state of the program, and nouns if they're used to return a certain value.

like image 161
Carl Smotricz Avatar answered Sep 22 '22 07:09

Carl Smotricz


One more important thing to do when writing a library is to use the same word to describe the same action every time. don't write a function named getName in one class and another function named retrieveNumber in another class.

like image 36
Moshe Levi Avatar answered Sep 22 '22 07:09

Moshe Levi