基于流行度的命名功能

发布于 2024-10-11 12:16:31 字数 239 浏览 3 评论 0原文

您需要两个函数:
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 技术交流群。

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

发布评论

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

评论(3

π浅易 2024-10-18 12:16:31

不,我不考虑函数的受欢迎程度。最好为这两个函数提供描述性名称(例如,一个是 plain_text(),一个是 rich_text())。

我认为最好为所有函数使用相对具体的名称,因为使用更通用的名称 1.) 不会让用户通过阅读名称对函数的功能有太多了解,并且 2.) 可能会导致混乱。

当然,如何命名函数是您的选择:我只是建议您为它们提供一些描述性的名称,并且一致地命名它们(并对参数进行排序)。

我最讨厌的就是使用 API 进行编程,其中每个函数名称都简短而神秘(PHP 的字符串函数就是这样,尽管我现在已经习惯了 - strstrstrtok< /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 is rich_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 and strtok are hardly intuitive names for what they do).

淡淡の花香 2024-10-18 12:16:31

有时,如果代码要被广泛使用,就值得考虑受欢迎程度。自然语言中的常用词往往很短。

然而,除非您认为您正在编写下一个 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() and getRichText().

思念满溢 2024-10-18 12:16:31

根据经验,我总是根据函数的用途来宽松地命名它们。如果我处于您的位置,我会将函数一命名为 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 two retrieve_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.

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