views:

88

answers:

5

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?

A: 

Have a look at

astander
+1  A: 

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.

Moshe Levi
Good tip, I'll certainly follow the same naming all way through my code.
rFactor
+3  A: 

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.

Carl Smotricz
That's very interesting!
rFactor
A: 
  1. Naming conventions for Java
  2. Naming conventions for .Net
  3. Naming conventions for C++
Suraj Chandran
A: 

Other prefixes? Possibly "isa", though that is only applicable in some situations.

Some languages can communicate "get" and/or "set" with other constructs (specifically, in Common Lisp you can make (setf (get* ...) blah) do the same as what you would've wanted (set* ... blah) do).

Vatine