我该如何命名这个简单的方法呢?
首先,这实际上并不是一个编程问题。这更像是一个“我如何满足 McConnels 命名约定?”的问题。
我有这个处理 Word 文档的 Delphi 应用程序。我需要做的一件事是遍历文档中的所有书签,并根据一个简单的规则删除其中一些书签:如果我当前处理报价,我将删除名称以“cw_orderspecic”开头的所有书签。如果我处理订单确认,我将删除所有名称以“cw_quotespecic”开头的书签。
该方法已就位,一切正常,但我有一个小问题。我应该如何称呼我的方法?当前名称(“DeleteBookmarksNotAllowedForCurrentDocumentType”)太长。
有什么建议吗?
First of all, this is not really a programming question. It's more a question of "how do I satisfy McConnels naming conventions?"
I have this Delphi-application that manipulates word-documents. One of the things I need to do is run through all the bookmarks in the document and delete some of them based upon a simple rule: If I currently work on a quote I will delete all bookmarks where the name begins with "cw_orderspecific". If I work on an order confirmation I will remove all bookmarks with names beginning with "cw_quotespecific".
The method is in place, and everything works perfectly, but I have a tiny problem. What should I call my method? The current name ("DeleteBookmarksNotAllowedForCurrentDocumentType") is too long.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更改函数以获取要删除的前缀,并将其命名为
DeleteBookmarksWithPrefix
:或
这使得代码非常可读,并且使该函数可以在其他情况下重用,而不是仅对一个非常特定的情况有用任务。
请注意,如果您有第三种工作类型,您可能需要颠倒它的含义:
Change the function to take the prefix to delete, and call it
DeleteBookmarksWithPrefix
:or
That makes the code very readable, and it makes the function reusable in other circumstances, rather than only ever being useful for one very specific task.
Note that if you'll ever have a third work type, you might want to reverse the sense of it:
删除不相关的书签?
DeleteNonRelevantBookmarks ?
你想要多短?我的第一反应是放弃一些看起来不那么重要的话。就像DeleteBookmarksNotAllowedForDocumentType、DeleteBookmarksNotAllowedForDocument 或简单地DeleteBookmarksNotAllowed 一样。虽然我可能更喜欢像DeleteInvalidBookmarks 这样的东西。当然,这仅在没有其他方法按不同规则删除书签的情况下才有效。
或者,如果您愿意改变方法函数的方式,我会通过将前缀作为参数来使其更加通用,然后命名会更简单。
How short you want it to be? My first instinct is to drop some words that don't seem that important. Like DeleteBookmarksNotAllowedForDocumentType, DeleteBookmarksNotAllowedForDocument, or simply DeleteBookmarksNotAllowed. Though I'd probably prefer something like DeleteInvalidBookmarks. Of course this works only if there are no other methods that delete bookmarks by different rules.
Or if you're willing to change the way you method functions, I'd make it more generic by giving the prefix as a parameter, then the naming would be simpler.
使用缩写词?在注释中定义 CDT 是“当前文档类型”,您将得到
DeleteBookmarks_NotAllowedForCDT
。够短了。Use acronyms? Define in comments that CDT is "Current Document Type", and you'll get
DeleteBookmarks_NotAllowedForCDT
. Short enough.DelBookmarksStartingWith
怎么样?说得很清楚,也表达了意图。How about
DelBookmarksStartingWith
? It's pretty clear and expresses intent.