Naming conventions for pure functions

functional programmingfunctionsnamingnaming-standardspure-function

Are there 'conventions' / best practices on naming pure functions?

For example:

  • adding numbers: add or sum?
  • calculating the square root: calcSqrt or sqrt?
  • reversing a list: reverse or reversed?
  • sorting a list: sort or sorted?

What drives such decisions? Is it a matter of brevity? Does it depend on the inputs, outputs and / or the contents (expressions) of the function body?

Would the use of nouns / adjectives as function names further reinforce concepts like referential transparency and declarative programming?

Best Answer

There are no "official" conventions.
But you definitely want to be consistent in the way how you name functions.

Name should be comprehensible in the context where you call it.

Good approach would be follow conventions of the framework or programming language you are using.

For example c# LINQ extension methods are pure functions and their names describe what action will be applied for given object.

On other hand in Ruby(Railish) language, method names tell what result will be produced.

Note that all languages have examples of both approaches, verb and noun.

Do no try to come up with one general rule for all cases. Give a name which do not force reader to open a function to understand what it is doing.

Use "right tool for the job"

Related Topic