2

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?

  • Generally, I would not use some like calcX. I would just use x (calcSum vs. sum). I'd also keep the names parallel. sum/difference, add/subtract not sum/subtract, add/difference, but they is my preference. Also, whatever you use, make sure the name actually means what it says. add for strings is not great, append or concatenate would be better. Sometimes input, sometimes output. Java has methods like add(x) addAll(Collection<x>), but also sometimes does the output thing (Collections.emptySet()). Clarity rules all. So I don't know how to give a general answer. – Kristian H Aug 20 '18 at 1:20
  • 1
    Prefer verbs over pronouns or nouns. In essence convey the function as an action rather than the attributes of the output data. – S.D. Aug 20 '18 at 7:33
  • @S.D. Why? What is the reason to explicitly convey a function as an action, rather than just another expression? It's not that I don't agree with you. It's about being able to explain to myself why the use of verbs is the most appropriate choice. – Willem-Aart Aug 20 '18 at 15:52
  • @Willem-Aart Technical reasons aside, good naming conventions allow to establish patterns for better understanding of source code by other developers. Aptly naming functions as verbs is natural. var decoded = decode(msg) vs var decode = decoded(msg). – S.D. Aug 21 '18 at 7:24
4

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"

  • reversed and sorted are neither verb nor noun. – Deduplicator Aug 19 '18 at 23:16
  • @Deduplicator Good point. One could argue that nouns and adjectives are in the same category within the context of this question. – Willem-Aart Aug 19 '18 at 23:33
  • I would disagree that there are no "official" naming conventions. There could be (and probably is at least one) language out there, where such an official (official being dictated e.g. by the inventor/maintainer of the language) convention exists. But there is no such convention across languages. – Graipher Oct 29 '18 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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