函数命名困难
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对于命名函数,请避免使用简单的名词,而应以动词命名。一些提示:
validateInput()
和validateUserInput()
,因为很难说一个函数比另一个函数做什么。另外,避免使用看起来非常相似的字符,例如数字 1 和小写“l”。有时它会有所作为。constructCarAndRunCar()
,而是让一个函数来构造它,另一个函数来运行它。如果您的函数介于 20 到 40 行之间,那就很好了。sim_pauseSimulation()
和sim_restartSimulation()
。如果您的课程是基于 OOP 的,那么这不是一个问题。addToVector()
或addToArray()
这样的函数,而是使用addToList()
。如果这些是原型或者数据结构以后可能会发生变化,则尤其如此。快乐编码! :)
For naming functions, just avoid having simply nouns and rather name them after verbs. Some pointers:
validateInput()
andvalidateUserInput()
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.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.sim_pauseSimulation()
andsim_restartSimulation()
. If your class is OOP-based, this isn't an issue as much.addToVector()
oraddToArray()
, have them beaddToList()
instead. This is especially true if these are prototypes or the data structures might change later.Happy coding! :)
尽力而为,如果仍然不合适,稍后再重新考虑。
Give it your best-shot and re-factor later if it still doesn't fit.
有时可能是您的函数太大,因此做了太多事情。尝试将您的函数拆分为其他函数,这样可能会更清楚如何调用每个函数。
不必担心用一两个单词来命名事物。有时,如果函数执行的操作可以用简短的句子来解释,那么可以将函数命名得更长一些,这样可以帮助其他开发人员理解正在发生的事情。
另一个建议是获取其他人的反馈。通常,从另一个角度来看并且第一次看到该函数的其他人会更好地了解如何调用该函数。
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.
我遵循以下规则:根据目的命名(为什么? - 设计决策)而不是内容(什么,如何? - 可以在代码中看到)。
对于函数来说,它几乎总是一个动作(动词),后跟参数名词和(或结果)。(离题但对于变量不要使用“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)
我发现当我不必减少单词时,命名函数会更容易。只要你不为 google 起始页执行 javascript,你就可以使用更长的名称。
例如,您在苹果可可框架中有方法
dequeueReusableCellWithIdentifier
和mergeChangesFromContextDidSaveNotification
。只要清楚该函数在做什么,您就可以将其命名为您想要的任何名称,并在以后重构它。
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
dequeueReusableCellWithIdentifier
andmergeChangesFromContextDidSaveNotification
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.
几乎与函数名称一样重要的是,您与注释保持一致。许多 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.
访问 www.thesaurus.com 并尝试通过同义词找到更适合的名称。
Go to www.thesaurus.com and try to find a better suited name though synonyms.
作为我自己的实用规则,如果函数名称太长,则应将其原子化为新对象。然而,我同意上面所有的帖子。顺便说一句,很好的菜鸟问题
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