I often have naming problems when I write classes and functions. Generally speaking, I try to follow the following set of rules if I have to deal with a function whose name is a verb:
- If the function alters the parameters, use an infinitive.
- Otherwise, use a present participle.
- If the function is a boolean, use
is
,has
orcan
before the infinitive.
I thought that this rule made sense, but looking at standard libraries of several programming languages, I feel rather alone. For example:
- Python's string methods do not alter the strings but are name with infinitive verbs (e.g.
capitalize
,split
,strip
...). - D's
std.functional
functions return new functions but are named with infinitive verbs too (e.g.memoize
,curry
...).
While it would have made sense to me to use strip
(had Python strings been mutable) to alter a string and stripped
to get the a new string (similar to the one passed but stripped from its leading and ending spaces), it seems that such a convention is almost never used (however, Python also provides the built-in functions reversed
and sorted
which do use past participle to tell that they respectively return a reversed or sorted collection without modifying the one passed to them). Are there contraindications concerning the use of past participle for the names non-altering functions and methods?
EDIT: I fail to see what is unclear, so I'll sum up the question in a few words:
Is it bad pratice to use past participle instead of imperative or infinitive verbs to name functions that take a single parameter, do not alter it, but return an altered copy of the said parameter?
ob.inverser(a)
orob.inversez(a)
. Maybe the French practice is to use the infinitive to avoid the formal/personal distinction (?) – Rob Y Mar 20 at 23:31ob.inverser()
; imperative would be awkward. Butob.inversé()
would make sense though. – Morwenn Mar 20 at 23:32obj.inverse()
, the noun form, which would also be valid in such a case. – Morwenn Mar 20 at 23:35