因此,我正在处理这个类,它应该通过web服务从供应商请求帮助文档。我试着把它命名为documententretriver, VendorDocRequester, DocGetter,但它们听起来不太对。最后,我在dictionary.com网站上浏览了半个小时,试图找到一个合适的词。
带着坏名字开始编程就像早上头发很糟一样,接下来的一天就会每况愈下。感觉我吗?
因此,我正在处理这个类,它应该通过web服务从供应商请求帮助文档。我试着把它命名为documententretriver, VendorDocRequester, DocGetter,但它们听起来不太对。最后,我在dictionary.com网站上浏览了半个小时,试图找到一个合适的词。
带着坏名字开始编程就像早上头发很糟一样,接下来的一天就会每况愈下。感觉我吗?
当前回答
上个月我刚刚写了一篇关于命名惯例的文章:http://caseysoftware.com/blog/useful-naming-conventions
要点是:
verbAdjectiveNounStructure -结构和形容词作为可选部分
对于动词,我坚持使用动作动词:保存、删除、通知、更新或生成。偶尔,我也会使用“进程”一词,但只是专门指队列或工作积压。
对于名词,我使用与之交互的类或对象。在web2project中,这通常是任务或项目。如果是Javascript与页面交互,则可能是body或table。关键在于代码清楚地描述了与之交互的对象。
该结构是可选的,因为它对该情况是惟一的。列表屏幕可能会请求List或Array。在web2project的项目列表中使用的核心函数之一就是getProjectList。它不修改底层数据,只修改数据的表示形式。
The adjectives are something else entirely. They are used as modifiers to the noun. Something as simple as getOpenProjects might be easily implemented with a getProjects and a switch parameter, but this tends to generate methods which require quite a bit of understanding of the underlying data and/or structure of the object... not necessarily something you want to encourage. By having more explicit and specific functions, you can completely wrap and hide the implementation from the code using it. Isn't that one of the points of OO?
其他回答
线程1:
function programming_job(){
while (i make classes){
Give each class a name quickly; always fairly long and descriptive.
Implement and test each class to see what they really are.
while (not satisfied){
Re-visit each class and make small adjustments
}
}
}
线程2:
while(true){
if (any code smells bad){
rework, rename until at least somewhat better
}
}
这里没有Thread.sleep(…)。
这也是为什么每个软件开发人员都应该具备写作和沟通技能的另一个原因。
我认为丰富的词汇量也很重要。
我能感受到你的痛苦。:/
我希望有一种工具可以与数据字典(描述各种变量/方法名称的文件,我猜有点像javadoc)一起检查源代码,这样你就可以编写这样的代码:
class Battery
{
double I; // current
double T; // temperature
double V; // voltage
double Q; // charge
void update(double Inew, double dt) { I = Inew; Q += I*dt; }
// ... etc ...
};
and the code-reviewing tool could do a number of different things to make it easier to view code in context, including display reminders that I = current (e.g. in a pane on the right-hand-side of the window it would display variable definitions/semantics/comments for the place in the code you are clicking on), or even allow you to do "virtual refactoring" where as a code reviewer you could rename something to your liking for readability/display reasons without actually changing the code stored on disk.
就像我喜欢自我描述名字一样,我讨厌读像BatteryFilteredCurrentInMilliamps这样的东西。通常在嵌入式系统中,我们是基于代数方程来建模对象的,这样的方程名称非常麻烦。(另一方面,“I”上面有一顶帽子,下标“d”和上标“*”是相当令人困惑的。)
我首先是一个EE /系统工程师,负责少量的软件工作,最后我真的不关心变量的名称,只要我有一种方便的方法来告诉它是什么,并将它映射到我自己的被控制系统的内部模型。
当每个合理的名字看起来都太长或不明确时,你可以尝试使用一些不太合理的名字,例如:
类GoForHelpLassie 类DunnoAskTechSupport RTFVM类[V为Vendor]
确保名称是唯一的,并且在类的顶部有一个描述性注释,因为任何在代码中看到它的人都需要查找它来找出它是做什么的(但是当他们这样做时,他们可能会发现它更容易记住)。
简而言之: 我同意好名字很重要,但我不认为你必须不惜一切代价在实现之前找到它们。
当然,最好一开始就有个好名字。但如果你不能在2分钟内想出一个,以后重命名将花费更少的时间,从生产力的角度来看是正确的选择。
长: 一般来说,在实现之前对名称考虑太久通常是不值得的。如果你实现你的类,命名它为“Foo”或“Dsnfdkgx”,在实现的时候,你会看到你应该给它命名什么。
特别是在Java+Eclipse中,重命名一点也不麻烦,因为它会小心地处理所有类中的所有引用,并警告您名称冲突等。只要这个类还没有在版本控制存储库中,我不认为重命名5次有什么错。
基本上,这是一个如何思考重构的问题。就我个人而言,我喜欢它,尽管它有时会惹恼我的队友,因为他们相信永远不要碰一个运行系统。从您可以重构的所有内容来看,更改名称是您可以做的最无害的事情之一。