基于流行度的命名功能
您需要两个函数:
1. 从数据库中检索纯文本。
2. 基于 1,检索富文本 - 使用新行、字体样式等。
您预计函数 2 比函数 1 更常用。
您会命名 1 text() 和 2 - rich_text() 还是使用更简单的text() 作为 2 的名称,因为预计它会更流行并使用类似 plain_text() 的内容作为 1?
更普遍的问题是 - 在命名函数时,您是否考虑了函数的预期“受欢迎程度”?
You need two functions:
1. Retrieves plain text from a DB.
2. Based on 1, retrieves rich text - with new lines, font styles etc.
You expect function 2 to be much more used than function 1.
Would you name 1 text() and 2 - rich_text() or would you use the simpler text() for the name of 2, since it's expected to be more popular and use something like plain_text() for 1?
The more general question would be - do you consider function's expected "popularity" when naming it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,我不考虑函数的受欢迎程度。最好为这两个函数提供描述性名称(例如,一个是
plain_text()
,一个是rich_text()
)。我认为最好为所有函数使用相对具体的名称,因为使用更通用的名称 1.) 不会让用户通过阅读名称对函数的功能有太多了解,并且 2.) 可能会导致混乱。
当然,如何命名函数是您的选择:我只是建议您为它们提供一些描述性的名称,并且一致地命名它们(并对参数进行排序)。
我最讨厌的就是使用 API 进行编程,其中每个函数名称都简短而神秘(PHP 的字符串函数就是这样,尽管我现在已经习惯了 -
strstr
和strtok< /code> 对于它们所做的事情来说很难是直观的名称)。
No, I don't consider a function's popularity. It's best to have descriptive names for both functions (e.g. one is
plain_text()
and one isrich_text()
).I think that it's best to use relatively specific names for all functions, since using more general names 1.) doesn't give the user too much of an idea of what the function does by reading the name and 2.) can lead to confusion.
Of course, how you name your functions is your choice: I just recommend that you give them somewhat descriptive names and that you name them (and order the arguments) consistently.
I hate nothing more than programming with an API where every function name is short and cryptic (PHP's string functions are that way, even though I'm used to it now --
strstr
andstrtok
are hardly intuitive names for what they do).有时,如果代码要被广泛使用,就值得考虑受欢迎程度。自然语言中的常用词往往很短。
然而,除非您认为您正在编写下一个 UNIX,否则您最好使名称具有描述性,而不必担心长度。
我会选择
getPlainText()
和getRichText()
。It's worth thinking about popularity, sometimes, if the code is going to be very widely used. Common words in natural languages tend to be short.
Unless you think you're writing the next UNIX, however, you're probably better off to make the names descriptive and not worry about length.
I'd go for
getPlainText()
andgetRichText()
.根据经验,我总是根据函数的用途来宽松地命名它们。如果我处于您的位置,我会将函数一命名为
retrieve_plain_text()
,将函数二命名为retrieve_rich_text()
。现在,我可以看一眼任一函数名称,并立即对该函数应该执行的操作有一个基本的了解:检索(从某些内容中获取,在本例中为数据库)纯/丰富(类型)文本。As a rule of thumb, I always name my functions loosely based on what they do. If I was in your shoes, I would name function one
retrieve_plain_text()
and function tworetrieve_rich_text()
. I can now glance at either function name and immediately have a basic understanding of what the function is supposed to do: Retrieve (get from something, in this case the database) plain/rich (the type) text.