4 ms·
When I see a subroutine with a verb in its name I think of side-effects. In my book a pure function should be named after the result it returns, so in this case
by augustk 6y ago
When I see a subroutine with a verb in its name I think of side-effects. In my book a pure function should be named after the result it returns, so in this case I would use the name `definition' instead of `find_definition' and `definition_url' instead of `build_url'. For predicates I try to avoid an "is" prefix when a simple adjective is sufficient.
- stingraycharles 6y agoI don’t think this is true at all. The most functional, side-effect-free functions are often just verbs (map, reduce, concat, etc).
- barrkel 6y agoMap, reduce, filter, fold, project, transform, group_by, bind, apply - any functional API you care to look is all verbs. Functions do work. That doesn't mean they have to have side-effects; if they're functional, they do work on the input and produce output. Doing is a verb. It's natural. Using a noun as a function name is at best justified when you have a situation where you want to hide whether data is being calculated on demand, or fetched from some storage or lookup table. Some languages bake this in, in the form of properties - attributes of a structure which look like fields, but are actually functions. Those things have nouns as names.
- augustk 6y agoIt depends on the culture and the programming language. The advantage of using noun phrases for pure functions is that 1. only by reading the name of the function you know it's a pure function 2. a call to a pure function in an expression reads more naturally since operands are values and values are nouns.
- ivanche 6y agoToo bad you're heavily downvoted. Principle "function name is a noun if it returns something, and verb if it doesn't" is super useful - just by looking at its name you know immediately if it has side-effects. Since I learned it I apply it all the time in programs I write alone, but almost always I see pushback in a team because people are unfortunately used to see `getX`, `fetchY` etc. as method names.
- augustk 6y agoI guess a verb phrase comes up naturally when you are eager to implement a new function and think of all the steps required to calculate the result. However, a noun phrase is a more proper abstraction since the implementation may change to simply return a cached value. (see also the Uniform Access Principle). Anyway, here is my naming strategy: 1. Boolean pure function Use an adjective phrase where the adjective is the last word, for instance UrlValid or DefinitionFound. 2. Non-boolean pure function Use a noun phrase where the noun is the last word, for instance CurrentDefinition or DefinitionUrl. 3. Non-pure "function" Use a verb phrase where the verb is the first word, for instance PrintError or ReadInput.