views:

181

answers:

7

I've noticed that it's common (at least in ECMAScript) to suffix event handlers with 'handler': clickHandler, fooBarHandler, etc… But I've always thought it made more sense to prefix them with 'handle': handleClick, handleFooBar, etc.

With prefix notation, methods are much easier to visually parse (it's very easy to distinguish between handlers and other things), faster to find (or ignore) with the editor's text completion and they adhere to the convention that methods should be verbs (handleClick is a verb, clickHandler is a noun).

So, why is the suffix notation common? Is there some hidden benefit that I haven't noticed?

+2  A: 

Who knows for sure, but I would guess that since they're called event handlers, following that pattern makes sense.

As in, eventHandler.

Robert S.
+1  A: 

For me suffix syntax (ClickHandler) looks more natural. It doesn't interfere with text completion much since you would usualy search for operation (as in ClickHandler) and not searching for which ever handler is supported. With that noted, Visual Studio 2010 supports text completion by suffix also.

Josip Medved
+1  A: 

Handle sounds more imperative - like it's a function you specifically call to do something. A handler is more asynchronous and outside of the direct flow of your program.

Michael
+3  A: 

It doesn't matter. Do what you like and get in religious debates with your co-workers.

Tom Ritter
+2  A: 

I would suggest that one reason would be to avoid confusion with handles, which Wikipedia calls "a particular kind of smart pointer." A handler is very much different. It's probably less of an issue in naming methods -- which are obviously not pointers of any sort -- than it is naming the internal variable that stores the method reference.

tvanfosson
+1  A: 

The traditional Flash naming convention uses "on" as a prefix rather than "Handler" as a suffix. Some people prefer verb method names, some prefer noun method names. The name onMouseClick is more of an adverb, mouseClickHandler is a noun which sounds like a class name, and handleMouseClick is a verb.

I typically use the "Handler" suffix to follow the Adobe Flex coding conventions, but the "on" prefix is much shorter and has the (already mentioned) benefit of sorting. If you are in Flex Builder hit Ctrl-O and it will popup a shortcut menu and typing just "on" will show you every handler in the file.

Jonathan Branam
+1  A: 

The term "handle" already has a meaning as an opaque pointer/value used to indirectly refer to a specific data structure of some kind. It is used in object oriented code to indirectly interact with the data structure through a set of library functions. A good example of a handle is the C language FILE* type, which is actually just an enumerated integer value on x86.

Hence it is a bit annoying to see it elsewhere. That's probably all it is.

Michael Speer