如果有一天,有人告诉你,你将来需要和一帮只会说英语和泰语的家伙工作在一起,而你的工作是要告诉他们如何写整洁的代码,如何做重构,什么是TDD,什么是面向对象,你该怎么办?
你问我,我只能告诉你,我需要重新读一次《整洁代码》,《重构》以及《
敏捷软件开发:原则、实践和模式》的英文版,然后整理出里面可以为我使用的术语和表达。
第三章:Comments(注释)
Expressive enough(足够的表达力)
Comments are not like Schindler’s List(辛德勒的名单). They are not “pure good.” Indeed, comments are, at best, a necessary evil. If our programming languages were expressive enough, or if we had the talent to subtly wield those languages to express our intent, we would not need comments very much—perhaps not at all.
They lie(注释会说谎)
Why am I so down on comments? Because they lie. Not always, and not intentionally, but too often. The older a comment is, and the farther away it is from the code it describes, the more likely it is to be just plain wrong. The reason is simple. Programmers can’t realistically maintain them.
Explain yourself in code(用代码解释)
// Check to see if the employee is eligible for full benefits
if ((employee.flags & HOURLY_FLAG) &&
(employee.age > 65))
Or
if (employee.isEligibleForFullBenefits())
Don’t Use a Comment When You Can Use a Function or a Variable(当你可以用一个函数或者变量时,不要使用注释)
The author of the original code may have written the comment first (unlikely) and then written the code to fulfill the comment. However, the author should then have refactored the code, as I did, so that the comment could be removed.
Commented-Out Code is not good practice(注释掉代码不是一个好的实践)
Few practices are as odious as the commenting-out code. Don’t do this!
Others who see that commented-out code won’t have the courage to delete it. They’ll think it is there for a reason and is too important to delete. So commented-out code gathers like dregs at the bottom of a bad bottle of wine.
个人认为,还是不要写Comment为好,最主要的原因,维护成本太高,带来的好处基本没有
To be continued(未完待续)
网友评论