Are there 'conventions' / best practices on naming pure functions?
For example:
- adding numbers:
add
orsum
? - calculating the square root:
calcSqrt
orsqrt
? - reversing a list:
reverse
orreversed
? - sorting a list:
sort
orsorted
?
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?
1 Answer 1
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
andsorted
are neither verb nor noun.Deduplicator– Deduplicator2018年08月19日 23:16:16 +00:00Commented Aug 19, 2018 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– Willem-Aart2018年08月19日 23:33:46 +00:00Commented Aug 19, 2018 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– Graipher2018年10月29日 20:13:31 +00:00Commented Oct 29, 2018 at 20:13
Explore related questions
See similar questions with these tags.
var decoded = decode(msg)
vsvar decode = decoded(msg)
.