The Boy Scout Rule(97 Things Every Programmer Should Know 8/97)

原文链接: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

童子军有一条规则:“总是让露营地比你来的时候更干净。”如果你发现地上一团糟,你就把它清理干净,不管谁可能把它弄得一团糟。你有意为下一组露营者改善环境。事实上,球探之父罗伯特·斯蒂芬森·斯迈斯·巴登·鲍威尔写的这条规则的最初形式是“试着离开这个世界,让它比你发现的好一点。”
如果我们在代码中遵循类似的规则:“提交一个模块的代码的时候,总要让它比下载的时候更简洁一些。”无论原作者是谁,如果我们总是做出一些努力,无论多么微小,来改进模块,该怎么办。结果会是什么?
我认为,如果我们都遵循这个简单的规则,那么就再也不会见到我们的软件渐渐腐化。相反,我们的系统会随着进化而逐渐变得越来越好。我们也会看到团队关心整个系统,而不仅仅是个人关心自己的一小部分。
我认为这个规则要求不过分。在你提交代码的时候,你不必让每个模块都完美。你只需要让它比刚拉下来时好一点。当然,这意味着添加到模块中的任何代码都必须是干净的。这也意味着,在重新提交模块之前,您至少要清理一件其他事情。您可以简单地改进一个变量的名称,或者将一个长函数拆分为两个较小的函数。您可以打破循环依赖,或者添加一个接口来将策略与细节解耦。
坦率地说,这对我来说听起来很体面——比如上完厕所后洗手,或者把垃圾放进垃圾桶而不是掉在地板上。事实上,在法规中留下烂摊子的行为应该和乱扔垃圾一样被社会所接受。这应该是一件没有完成的事情。
但这还不止于此。关心我们自己的代码是一回事。关心团队的代码是另一回事。团队互相帮助,互相清理。他们遵循童子军规则,因为这对每个人都有好处,而不仅仅是对他们自己有好处。
作者:鲍勃叔叔

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值