8

There is a more generic question asked here. Consider this as an extension to that.

I understand that classes represent type of objects and we should use nouns as their names. But there are function objects supported in many language that acts more like functions than objects. Should I name those classes also as nouns, or verbs are ok in that case. doSomething(), semantically, makes more sense than Something().

Update / Conclusion

The two top voted answers I got on this shares a mixed opinion:

Attila

In the case of functors, however, they represent "action", so naming them with a verb (or some sort of noun-verb combination) is more appropriate -- just like you would name a function based on what it is doing.

Rook

The instance of a functor on the other hand is effectively a function, and would perhaps be better named accordingly. Following Jim's suggestion above, SomethingDoer doSomething; doSomething();

Both of them, after going through some code, seems to be the common practice. In GNU implementation of stl I found classes like negate, plus, minus (bits/stl_function.h) etc. And variate_generator, mersenne_twister (tr1/random.h). Similarly in boost I found classes like base_visitor, predecessor_recorder (graph/visitors.hpp) as well as inverse, inplace_erase (icl/functors.hpp)

Community
  • 1
  • 1
Vikas
  • 8,790
  • 4
  • 38
  • 48
  • Agree. Although they are termed as "function objects" they simply are "functions" more than "objects". So, I will go with your reasoning above that it should be named as "doSomething" i.e prefaced by a verb which semantically makes sense. In case you are using them as callbacks, and if the language that you work on supports closures/lambdas(I understand they are a tad bit different) use them so that you're free from the hassles of naming them :) – verisimilitude May 30 '12 at 11:34
  • I'm more than eager to know the opinions of the SO community on this :) – verisimilitude May 30 '12 at 11:47
  • Any updates on this question? – verisimilitude Jun 06 '12 at 12:19
  • @verisimilitude, started a bounty. – Vikas Jun 06 '12 at 14:36
  • I think this question could be improved by providing some specific examples of class names that represent callable objects. You provided some examples of a function name, but where's the class name it's attached to? – mpontillo Jun 06 '12 at 23:33

4 Answers4

6

Objects (and thus classes) usually represent "things", therefore you want to name them with nouns. In the case of functors, however, they represent "action", so naming them with a verb (or some sort of noun-verb combination) is more appropriate -- just like you would name a function based on what it is doing.

In general, you would want to name things (objects, functions, etc.) after their purpose, that is, what they represent in the program/the world.

You can think of functors as functions (thus "action") with state. Since the clean way of achieving this (having a state associated with your "action") is to create an object that stores the state, you get an object, which is really an "action" (a fancy function).

Note that the above applies to languages where you can make a pure functor, that is, an object where the invocation is the same as for a regular function (e.g. C++). In languages where this is not supported (that is, you have to have a method other than operator() called, like command.do()), you would want to name the command-like class/object a noun and name the method called a verb.

Attila
  • 28,265
  • 3
  • 46
  • 55
  • 1
    If the function is the action, then the function is the verb, not the class, right? In the example cited, I'd probably use "SomethingDoer", with a method "Do" or "DoSomething" – ssis_ssiSucks Jun 06 '12 at 23:47
  • @JimMcKeon - Yes, you are right. In that case the class/object is the "state", so noun is more appropriate, where the function is the "action" you perform on the state, so use a verb for that. – Attila Jun 07 '12 at 00:27
  • Thanks for the answer. This and answer by @Rook are the two ways function object classes are typically named (check my update in question). – Vikas Jun 13 '12 at 08:34
3

The type of the functor is a class, which you may as well name in your usual style (for instance, a noun) because it doesn't really do anything on its own. The instance of a functor on the other hand is effectively a function, and would perhaps be better named accordingly. Following Jim's suggestion above, SomethingDoer doSomething; doSomething();

A quick peek at my own code shows I've managed to avoid this issue entirely by using words which are both nouns and verbs... Filter and Show for example. Might be tricky to apply that naming scheme in all circumstances, however!

Rook
  • 5,734
  • 3
  • 34
  • 43
  • Thanks for the answer. It is a good explanation and together with @Attila's answer gives a good option to name classes. Unfortunately I can not share bounty and I'll award it to Attila who answered before this. – Vikas Jun 13 '12 at 08:33
2

I suggest you give them a suffix like Function, Callback, Action, Command, etc. so that you will have classes called SortFunction, SearchAction, ExecuteCommand instead of Sort, Search, Execute which sound more like names of actual functions.

Attila
  • 28,265
  • 3
  • 46
  • 55
adranale
  • 2,835
  • 1
  • 21
  • 39
1

I think there are two ways to see this:

1) The class which could be named sensibly so that it can be invoked as a functor directly upon construction:

Doer()(); // constructed and invoked in one shot, compiler could optimize

2) The instance in cases where we want the functor to be invoked multiple times on a stateless functor object, or perhaps for stylistic reasons when #1's syntax is not preferable.

Doer _do;
_do(); // operator () invocation after construction

I like @Rook's suggestion above of naming the class with words that have the same verb and noun forms such as Search or Filter.

scorpiodawg
  • 5,612
  • 3
  • 42
  • 62