Function naming conventions

FunctionNaming Conventions

Function Problem Overview


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?

Function Solutions


Solution 1 - Function

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.

Solution 2 - Function

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.

Solution 3 - Function

Solution 4 - Function

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

Solution 5 - Function

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

Solution 6 - Function

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

Solution 7 - Function

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];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTowerView Question on Stackoverflow
Solution 1 - FunctionCarl SmotriczView Answer on Stackoverflow
Solution 2 - FunctionMoshe LeviView Answer on Stackoverflow
Solution 3 - FunctionAdriaan StanderView Answer on Stackoverflow
Solution 4 - FunctiongamlielaView Answer on Stackoverflow
Solution 5 - FunctionVatineView Answer on Stackoverflow
Solution 6 - FunctionFranMowinckelView Answer on Stackoverflow
Solution 7 - FunctionVlad AlivanovView Answer on Stackoverflow