It’s “onBar” or “foo”, not “slotFoo”.

What would you think if you find somebody naming a function “publicDoSomething”? Or “voidDoSomething”? Or even “protectedVoidDoSomethingConst”?

It may be well what I think if these days I see somebody naming a function “slotDoSomething”. Although when I started with Qt programming you could see me giving similar names here and there. But this was just because I was following the great principle of Doing New Things By Example ™. And obviously some people had started before to apply some kind of partitial verbose hungarian notation to method names. And a funny thing about pattern based learning systems, like I and surely others are/were, is that they take correlating variations as hints to rules. And start to overdo: We see some methods that are slots being prefixed with “slot”, well, so we prefix them all. No idea, who started when and why, but now it’s there.

And I consider it to make the resulting code dirty. Being a slot is just a property of a method. Like “const”, the visibility, the return type or the types and number of parameters. All the latter properties is not referred to in the naming of the method, at least in the code I know. So why the being-a-slot property? I see no advantages, only disadvantages.

Methods that are given the property “slot” can be divided into two groups:

  • methods with a semantic to others
  • methods with a semantic to only the object itself

The first ones are functions which have a defined operational semantic, reflected in the method name, like “doFoo”. So other objects can call this method to achieve something. They are also set as slots because the operations done by the method make sense to be synchronized to certain events, by signals. Still the name is only based on the operational semantic. These functions are to be called by others (if only subclasses), so they are visible public or protected, e.g.:

class FooBar {
public Q_SLOTS:
void doFoo();
protected Q_SLOTS:
void doMoreFoo() const;
};

If one wants to synchronize an operation of the object to an event of another object, it writes:
connect( anotherObject, SIGNAL(bar()), object, SLOT(doFoo()) );

The latter ones differ in that they are methods whose only purpose is to catch dedicated signals. Signals with a given semantic. As reflected in the signal name, like “Done”. No other object will ever call these method, perhaps even not the object itself. It is only connected to the signal and called on it’s emission. So the semantic of the method is react-on-signal-x. In other languages like Javascript or Delphi those methods have a naming pattern: “onSomething” for the signal “something”. Makes much sense to me. Why should Qt programming be done differently?
Because these method can be only sensefully used by the class itself they are visible private, e.g.:

class FooBar {
private Q_SLOTS:
void onBar() const;
};

So connecting is done within the object by:

connect( anotherObject, SIGNAL(bar()), SLOT(onBar()) );
How beautiful is this code?

Think about it. A slot is just a property. Name your functions after the semantic, if is available to others, or the signal. Omit the property.
Well, or just disagree 😛

Hm, not yet convinced? Than take your final push:
“on” is two letters less to type. And two characters less to compare on function lookup, so results in faster linking. A little, at least. 😉 And without “slot” it is even four letters less.

Now you are convinced, right? Then go and improve your KDE code. Now! 🙂

Updated:
From your comments I guess I was not too clear, hope the added text now helps to understand better what I mean. Some things should simply get some more days to be written, sigh.

Advertisements

5 thoughts on “It’s “onBar” or “foo”, not “slotFoo”.

  1. nope, sorry not convinced. the ‘onBar’ naming convention you suggest is used on windows and frankly it’s just as ugly here as it is there. Function names should be descriptive enough that we shouldn’t have to resort to silly prefixes for things whether they’re slots or not (since slots can be called without being triggered by a signal)

  2. I think the “on” prefix is ugly as h*ll. Perhaps it’s because I’m not a native speaker, but it doesn’t give me the sense of an event handler at all.

    I tend to use normal function names for public slots (because it’s indeed ugly), and prefix methods with “slot” when they are private event handlers. It’s some indication the method will be called automatically from some point, because you won’t see a function call anywhere.

    Sort naming don’t interest me. I’m used to use readable names, even if they are long. Like like acronyms, it works counter productive when you see the code again a few weeks later. 😉 (granted “on” is not a acronym, but the argument it’s 2 letters less to type is not really important to me)

  3. The “on” convention definitely does match English usage; “on doing X” means the same as “when X is/was done”. From my experience of learning other languages (German and Arabic), prepositions often don’t stay the same from one language to another.

    I don’t use the particular notation you mention (I sometimes put “// slot” at the end of the opening line), but I do use Hungarian notation when naming Qt widgets (but not primitives), simply as a reminder to me when returning to the application after not looking at it for a while; I also find that “pbOk” is more succinct than “OkButton”. The only snag is when two widgets have similar initials (like check-box and combo-box).

  4. I personally highly dislike when people name their functions like the signal they are connected to. To me a function name should describe an action[1]. ‘slotChanged()’ or ‘onChanged()’ look like an action triggered as a result of a ‘changed()’ signal. Naming the function ‘changed()’ is just weird.

    Still, the best solution IMO is to name the function after what it does, rather than after what it is connected to, but it’s not always convenient.

    [1] Except accessors of course

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s