为什么事件处理程序的后缀通常是“handler”? 为什么不给它们加上“句柄”前缀呢?

发布于 2024-08-01 18:03:36 字数 389 浏览 7 评论 0原文

我注意到,在事件处理程序中添加“handler”后缀是很常见的(至少在 ECMAScript 中):clickHandlerfooBarHandler 等……但我一直认为这使得更有意义的是用“handle”作为前缀:handleClickhandleFooBar等。

使用前缀表示法,方法更容易直观地解析(很容易区分处理程序和处理程序)其他东西),通过编辑器的文本完成更快地查找(或忽略),并且它们遵循方法应该是动词的约定(handleClick 是动词,clickHandler 是名词)。

那么,为什么后缀表示法如此常见呢? 是否有一些我没有注意到的隐藏好处?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

空城仅有旧梦在 2024-08-08 18:03:36

谁知道呢,但我猜由于它们被称为事件处理程序,遵循该模式是有意义的。

eventHandler 所示。

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

As in, eventHandler.

爱的那么颓废 2024-08-08 18:03:36

我建议原因之一是避免与 handles 混淆,维基百科称之为“一种特殊的智能指针。” 处理程序有很大不同。 命名方法(显然不是任何类型的指针)可能比命名存储方法引用的内部变量更不成问题。

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.

情定在深秋 2024-08-08 18:03:36

没关系。 做你喜欢做的事,与你的同事进行宗教辩论。

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

烂柯人 2024-08-08 18:03:36

传统的 Flash 命名约定使用“on”作为前缀,而不是“Handler”作为后缀。 有些人喜欢动词方法名称,有些人喜欢名词方法名称。 onMouseClick 这个名字更多的是一个副词,mouseClickHandler 是一个听起来像类名的名词,而handleMouseClick 是一个动词。

我通常使用“Handler”后缀来遵循 Adobe Flex 编码约定,但是“on”前缀要短得多,并且具有(已经提到的)排序的好处。 如果您在 Flex Builder 中,按 Ctrl-O,它将弹出一个快捷菜单,只需输入“on”即可显示文件中的每个处理程序。

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.

只想待在家 2024-08-08 18:03:36

术语“句柄”已经具有作为不透明指针/值的含义,用于间接引用某种特定的数据结构。 它用于面向对象的代码中,通过一组库函数间接与数据结构交互。 句柄的一个很好的例子是 C 语言的 FILE* 类型,它实际上只是 x86 上的一个枚举整数值。

因此,在其他地方看到它有点烦人。 大概就是这样。

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.

笑饮青盏花 2024-08-08 18:03:36

对我来说后缀语法(ClickHandler)看起来更自然。 它不会过多干扰文本完成,因为您通常会搜索操作(如 ClickHandler 中),而不是搜索支持哪个处理程序。 值得注意的是,Visual Studio 2010 还支持按后缀完成文本。

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.

青衫负雪 2024-08-08 18:03:36

Handle 听起来更加命令式 - 就像它是您专门调用来执行某些操作的函数一样。 处理程序更加异步,并且位于程序的直接流程之外。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文