函数命名困难

发布于 2024-09-07 08:14:20 字数 308 浏览 4 评论 0原文

可能的重复:
其他人发现命名类和方法是编程中最困难的部分之一?

有时我似乎无法真正为我正在编写的函数找到任何名称,这可能是因为该函数不够内聚吗?

当你想不出函数的好名字时你会怎么做?

Possible Duplicate:
Anyone else find naming classes and methods one of the most difficult part in programming?

Sometimes it seems i cant really find any name for a function i am writing, can this be because the function is not cohesive enough?

What do you do when no good name for a function comes to mind?

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

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

发布评论

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

评论(8

如果没结果 2024-09-14 08:14:20

对于命名函数,请避免使用简单的名词,而应以动词命名。一些提示:

  1. 具有明显唯一的函数名称,例如,没有 validateInput()validateUserInput(),因为很难说一个函数比另一个函数做什么。另外,避免使用看起来非常相似的字符,例如数字 1 和小写“l”。有时它会有所作为。
  2. 您正在与多人一起开展一个项目吗?您还应该花一些时间检查命名约定,例如函数名称是否应该有下划线、应该是驼峰命名法等。
  3. 匈牙利表示法是一个坏主意;避免这样做。
  4. 想想这个函数正在做什么。我想到了您在问题中提到的凝聚力。一般来说,函数应该只做一件事,所以不要将其命名为 constructCarAndRunCar() ,而是让一个函数来构造它,另一个函数来运行它。如果您的函数介于 20 到 40 行之间,那就很好了。
  5. 有时,这取决于项目,如果该类是纯过程性的(仅由函数组成),您可能还希望在函数名称前添加该类的前缀。因此,如果您有一个负责运行模拟的类,请将您的函数命名为 sim_pauseSimulation()sim_restartSimulation()。如果您的课程是基于 OOP 的,那么这不是一个问题。
  6. 不要在函数本身中使用底层数据结构;这些应该被抽象掉。不要使用像 addToVector()addToArray() 这样的函数,而是使用 addToList() 。如果这些是原型或者数据结构以后可能会发生变化,则尤其如此。
  7. 最后,命名约定保持一致。一旦你经过一番思考想出了一个约定,就坚持下去。当想到不一致的函数名称时,我们会想到 PHP。

快乐编码! :)

For naming functions, just avoid having simply nouns and rather name them after verbs. Some pointers:

  1. Have function names that are unique visibly, e.g. don't have validateInput() and validateUserInput() since it's hard to say what one does over another. Also, avoid having characters that look very similar, e.g. the number 1 and lowercase 'l'. Sometimes it makes a difference.
  2. Are you working on a project with multiple people? You should spend some time going over naming conventions as well, such as if the function name should have underscores, should be camelCase, etc.
  3. Hungarian notation is a bad idea; avoid doing it.
  4. Think about what the function is doing. The cohesion that you mentioned in your question comes to mind. Generally, functions should do just one thing, so don't name it constructCarAndRunCar() but rather have one function that constructs and another that runs it. If your functions are between, say 20 and 40 lines, you're good.
  5. Sometimes, and this depends on the project, you might also want to prefix your function names with the class if the class is purely procedural (only composed of functions). So if you have a class that takes care of running a simulation, name your functions sim_pauseSimulation() and sim_restartSimulation(). If your class is OOP-based, this isn't an issue as much.
  6. Don't use the underlying data structures in the functions themselves; these should be abstracted away. Rather than having functions like addToVector() or addToArray(), have them be addToList() instead. This is especially true if these are prototypes or the data structures might change later.
  7. Finally, be consistent in your naming conventions. Once you come up with a convention after some thinking, stick to it. PHP comes to mind when thinking of inconsistent function names.

Happy coding! :)

笔芯 2024-09-14 08:14:20

尽力而为,如果仍然不合适,稍后再重新考虑。

Give it your best-shot and re-factor later if it still doesn't fit.

以歌曲疗慰 2024-09-14 08:14:20

有时可能是您的函数太大,因此做了太多事情。尝试将您的函数拆分为其他函数,这样可能会更清楚如何调用每个函数。

不必担心用一两个单词来命名事物。有时,如果函数执行的操作可以用简短的句子来解释,那么可以将函数命名得更长一些,这样可以帮助其他开发人员理解正在发生的事情。

另一个建议是获取其他人的反馈。通常,从另一个角度来看并且第一次看到该函数的其他人会更好地了解如何调用该函数。

Sometimes it could be that your function is too large and therefore doing too many things. Try splitting up your function into other functions and it might be clearer what to call each individual function.

Don't worry about naming things with one or two words. Sometimes if functions do something that can be explained in a mini-sentence of sorts, go ahead and name the function a little longer if it'll help other developers understand what is going on.

Another suggestion is to get feedback from others. Often others who come from another perspective and seeing the function for the first time will have a better idea on what to call the function.

断桥再见 2024-09-14 08:14:20

我遵循以下规则:根据目的命名(为什么? - 设计决策)而不是内容(什么,如何? - 可以在代码中看到)。

对于函数来说,它几乎总是一个动作(动词),后跟参数名词和(或结果)。(离题但对于变量不要使用“arrayOfNames”或“listOfNames”,这些是类型信息,而只是“名称”)。如果您部分重构代码,这也将避免不一致。

对于给定的模式(例如对象创建),要一致并始终使用相同的命名,例如“创建...”(有时不是“分配...”或“构建...”,否则您或您的同事最终会挠头)

I follow following rule: Name according to the purpose (Why? - design decision) and not to the contents (What, How? - can be seen in the code).

For functions it is almost always an action (verb) followed by the noun of parameters and (or results. (Off-topic but for variables do not use "arrayOfNames" or "listOfNames", these are type information but simply "names"). This will also avoid inconsistencies if you refactor the code partly.

For given patterns like object creation, be consistent and always use the same naming like "Create..." (and not sometimes "Allocate..." or "Build..." otherwise you or your collegues will end up in scratching their head wound)

我做我的改变 2024-09-14 08:14:20

我发现当我不必减少单词时,命名函数会更容易。只要你不为 google 起始页执行 javascript,你就可以使用更长的名称。

例如,您在苹果可可框架中有方法 dequeueReusableCellWithIdentifiermergeChangesFromContextDidSaveNotification

只要清楚该函数在做什么,您就可以将其命名为您想要的任何名称,并在以后重构它。

I find it easier to name functions when I don't have to cut back on the words. As long as your not doing javascript for the google start page you can do longer names.

For example you have the method dequeueReusableCellWithIdentifierandmergeChangesFromContextDidSaveNotification in apples cocoa framework.

As long as it's clear what the function is doing you can name it whatever you want and refactor it later.

慈悲佛祖 2024-09-14 08:14:20

几乎与函数名称一样重要的是,您与注释保持一致。许多 IDE 将使用格式正确的注释,不仅为您可能正在使用的函数提供上下文相关的帮助,而且还可用于生成文档。当长时间后返回项目或与其他开发人员合作时,这是无价的

在学术环境中,他们可以很好地展示你的意图。

一个好的经验法则是 [verb]returnDescription。这对于 GetName() 类型函数来说很容易,但不能普遍应用。在不引人注目的代码和描述性代码之间找到平衡是很困难的。

这是.Net 约定指南,但它适用于大多数语言。

Almost as important as the function name is that you are consistent with comments. Many IDEs will user your properly formatted comments not only to provide context sensitive help for a function you might be using, but they can be used to generate documentation. This is invaluable when returning to a project after a long period or when working with other developers.

In academic settings, they provide an appreciated demonstration of your intentions.

A good rule of thumb is [verb]returnDescription. This is easy with GetName() type functions and can't be applied universally. It's tough to find a balance between unobtrusive and descriptive code.

Here's a .Net convention guide, but it is applicable to most languages.

腹黑女流氓 2024-09-14 08:14:20

访问 www.thesaurus.com 并尝试通过同义词找到更适合的名称。

Go to www.thesaurus.com and try to find a better suited name though synonyms.

表情可笑 2024-09-14 08:14:20

作为我自己的实用规则,如果函数名称太长,则应将其原子化为新对象。然而,我同意上面所有的帖子。顺便说一句,很好的菜鸟问题

As a practical rule of my own, if a function name is too long, it should be atomized in a new object. Yet, i agree with all posts above. btw, nice noob question

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