编程的97件事——8童子军军规
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编程的97件事——8童子军军规相关的知识,希望对你有一定的参考价值。
童子军军规
童子军有一条规则:“总是保证营地在离开时比发现时整洁。”如果你发现地上是乱的,不论是谁弄乱的,你都要收拾干净。你要有意的为一下批露营者改善营地环境。实际上这条规则,是童子军之父,Robert Stephenson Smyth Baden-Powell写的:“努力留下一个比你发现时更好的世界。”
如果我们在编码时也新遵循一条类似的规则:“总是保证模块签入时比签出时整洁”。无论模块的原作者是谁,如果我们总是作出一些努力,无论多么微小,去改进模块,结果会怎样?
我认为如果我们都能遵守这条简单的规则,我们就能看到我们的软件系统不再无情地恶化下去。取而代之的是,我们的系统会发展地越来越好。我们会看到团队将系统看作一个整体来关注,而不是每个成员只关心自己负责的那一小部分。
我认为这样的要求并不过分。你不必保证每个你要签入的模块是完美的。你只要让这个模块签入时比签出时好一点点。当然,这意味着你为这个模块添加的代码必须是干净的。这也意味着你签入模块前至少整理了一个模块中其他的部分。你也许只是改进了一个变量的命名,或是将一个长方法分割成了两个更小的方法。你可能中断了一个循环依赖,或增加了一个接口以解耦规则对细节的耦合。
坦白来说,这就像平常的得体做法一样——就像你上了厕所要洗手,或者要将垃圾扔进垃圾筒而不是地上。实际上留下混乱的代码就像乱扔垃圾一样,是不被社会所接受的。它本就不该发生。
不仅如此,关注我们自己的代码是一回事,关注团队的代码完全是另外一回事。团队成员互相帮助,互相整理代码。他们遵守童子军军规是因为这对每一个人都好,而不只是对自己有好处。
by Uncle Bob
The Boy Scout Rule
The Boy Scouts have a rule: "Always leave the campground cleaner than you found it." If you find a mess on the ground, you clean it up regardless of who might have made the mess. You intentionally improve the environment for the next group of campers. Actually the original form of that rule, written by Robert Stephenson Smyth Baden-Powell, the father of scouting, was "Try and leave this world a little better than you found it."
What if we followed a similar rule in our code: "Always check a module in cleaner than when you checked it out." No matter who the original author was, what if we always made some effort, no matter how small, to improve the module. What would be the result?
I think if we all followed that simple rule, we‘d see the end of the relentless deterioration of our software systems. Instead, our systems would gradually get better and better as they evolved. We‘d also see teams caring for the system as a whole, rather than just individuals caring for their own small little part.
I don‘t think this rule is too much to ask. You don‘t have to make every module perfect before you check it in. You simply have to make it a little bit better than when you checked it out. Of course, this means that any code you add to a module must be clean. It also means that you clean up at least one other thing before you check the module back in. You might simply improve the name of one variable, or split one long function into two smaller functions. You might break a circular dependency, or add an interface to decouple policy from detail.
Frankly, this just sounds like common decency to me — like washing your hands after you use the restroom, or putting your trash in the bin instead of dropping it on the floor. Indeed the act of leaving a mess in the code should be as socially unacceptable as littering. It should be something that just isn‘t done.
But it‘s more than that. Caring for our own code is one thing. Caring for the team‘s code is quite another. Teams help each other, and clean up after each other. They follow the Boy Scout rule because it‘s good for everyone, not just good for themselves.
by Uncle Bob
以上是关于编程的97件事——8童子军军规的主要内容,如果未能解决你的问题,请参考以下文章