57

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?

6
  • 6
    This is language-dependent to some degree, since different languages often have different naming/coding conventions.
    – skaffman
    Jan 2, 2010 at 11:26
  • 1
    getters and setters can be an indication of poor design, if you have too many of them.
    – anon
    Jan 2, 2010 at 11:28
  • @Neil Butterworth: Interesting! Curious to know why and what would be better approach then? (This is about getters and setters)
    – Madhu
    Jan 2, 2010 at 11:31
  • 2
    If you need many get/set functions, it is often an indication that your class is simply a "record" with no real behaviour.
    – anon
    Jan 2, 2010 at 11:34
  • @anon (Jan 2 '10 at 11:34) I don't see your point. The question is "What are good naming conventions?". Your answer seems to be a half-voiced disapproval of things like Data Access Objects?
    – Byson
    Aug 3, 2015 at 12:30

7 Answers 7

98

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.

8
  • 60
    so that means getter functions are all named wrong?... ie getSomeValue();
    – timh
    Aug 6, 2012 at 22:24
  • 1
    @timh Accessors are a bit of a special case, they usually return properties of an object, something you'd just use property access syntax for in C# or AS3.
    – futlib
    May 23, 2013 at 3:05
  • 2
    @timh yes they are wrong thats why C# fixed theirs with get;set property Dec 3, 2015 at 23:50
  • 8
    This isn't a good rule in languages with named closures and computed properties. A better rule for languages with those features is to always name functions as verbs or predicates. You have to think about what the function will look like when stored as a closure. A noun won't make sense; that gives the impression that it's the result of invocation.
    – Jessy
    Apr 30, 2018 at 3:44
  • 9
    A verb means action, functions do things. Using verbs for all function names is reasonable.
    – Yetti99
    Oct 24, 2019 at 19:28
16

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.

3
  • 5
    I typically use get when it gets the data locally from class variable and or has a calculation (e.g circle.getArea()), and then I use retrieve when it must get this data from the outside, (e.g from database: db.retrieveUserById(123) Dec 18, 2018 at 9:19
  • 1
    @DavidCallanan, how it the method of retrieval relevant to the caller?
    – ThaJay
    Mar 18, 2020 at 9:55
  • 4
    @ThaJay looking at this two years later, it probably isn't relevant to the caller where the data comes, and I would probably use get everywhere. Mar 19, 2020 at 12:15
4

Have a look at

3

Here is a great resource advising the same as @Carl's answer: https://swift.org/documentation/api-design-guidelines/#strive-for-fluent-usage

Name functions and methods according to their side-effects

  • Those without side-effects should read as noun phrases, e.g. x.distance(to: y), i.successor().

  • Those with side-effects should read as imperative verb phrases, e.g., print(x), x.sort(), x.append(y).

1
  • 5
    I find this a pretty bad convention, because so many words are both noun and verb. Even in the example: You can distance yourself from something, and obtain a print using some sort of paper. In practice I would have no clue which of x.view(), x.set(), or x.route() may have a side-effect.
    – bluenote10
    Apr 1, 2021 at 13:25
2

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).

0

If there is a universal rule, I think it should be to be consistent.

There is also the "on" prefix, which is widely used when dealing with events (i.e. Java Android: onViewCreated). Some other prefixes or short and/or generic verbs (such as has, get and set) widely used are:

I prefer using nouns for simple getters when there is very little logic involved (i.e. properties) but I would use the "get" prefix for complex actions:

func center() {
   return (a + b) / 2
}

However, in some languages where using explicitly the "get" prefix is widely extended (i.e. Android - Java), the common practice is using some verb such as "compute" (i.e. computeVerticalScrollOffset())

Furthermore, in some languages (e.g. swift) you can also use property setters so you don't really use the "set" prefix:

var x: X {
  get {
    return foo(x)
  }
  set {
    x = bar(newValue)
  }
}

// Set x
x = y

And finally, there are many widely used constructions such as instanceof, indexOf, ...

0

Pro get/set

When a class has many methods, it is better to use verb prefixes, such as get/set, to distinguish methods from each other.

PHP example:

$foo->setText('Hello world!');
$foo->prependText('So. ');
$foo->appendText(' And welcome');
$x = $foo->getText();

By the way, in Hungarian notation prefixes go with a small letter and will not detract from keyword.

Counter get/set

When you need only two methods, it is easier to use the same noun in the context of using parameters.

jQuery example:

$('.foo').html();                //get
$('.foo').html('Hello world!'); //set

Examples

For functions and static methods with arrays as parameters I use the following rule:

If changes should occur only at run time:

setFoo($arr); // Replace/delete all properties, i.e. if some elements are not passed, the corresponding properties will get empty values.
setFoo([]); // Delete all properties
setFoo(); // Set all properties by default
delFoo($arr); // Delete specified properties
addFoo($arr); // Add/replace specified properties

If changes will be made forever (in DB or files):

deleteFoo(...); // Delete specified properties
insertFoo(...); // Add specified properties
replaceFoo(...); // Add or replace specified properties
updateFoo(...); // Update specified properties

For both cases:

$arr = getFoo(); // Get all properties    
$val = getFoo($level1, $level2, ...); // You can obtain the value of the given level, placing the list of arguments
or
$val=getFoo()[$level1][$level2];

Not the answer you're looking for? Browse other questions tagged or ask your own question.