07谨慎提取共享代码
It was my first project at the company. I'd just finished my degree and was anxious to prove myself, staying late every day going through the existing code. As I worked through my first feature I took extra care to put in place everything I had learned — commenting, logging, pulling out shared code into libraries where possible, the works. The code review that I had felt so ready for came as a rude awakening — reuse was frowned upon!
这是我在公司的第一个项目。我刚毕业,急于证明自己,每天都熬夜加班写代码。当我完成我的第一个功能时,我特别用心的把我在学校学到的一切---包括注释、日志、尽可能地将重复代码抽取为共享库、编码。我本以为代码审核必过无疑,但实际却非如此,美梦被惊醒了---重用被禁止了!
How could this be? All through college reuse was held up as the epitome of quality software engineering. All the articles I had read, the textbooks, the seasoned software professionals who taught me. Was it all wrong?
这怎么可能?在整个大学里,重用一直被视为优质软件工程的缩影。所有我读过的文章,教科书,软件行业牛人的经验,这些都错了吗?
It turns out that I was missing something critical.
原来我遗漏了一些关键的东西。
Context.
背景。
The fact that two wildly different parts of the system performed some logic in the same way meant less than I thought. Up until I had pulled out those libraries of shared code, these parts were not dependent on each other. Each could evolve independently. Each could change its logic to suit the needs of the system's changing business environment. Those four lines of similar code were accidental — a temporal anomaly, a coincidence. That is, until I came along.
系统的两个截然不同的部分以相同的方式执行某些逻辑,这一事实比我想象的要少。在我我提取了共享代码库之前,这些部分并不相互依赖。每一个部分都可以独立进化。每一个部分都可以改变其逻辑,以适应系统不断变化的业务环境的需要。这四行类似的代码是偶然的——时间异常,巧合。也就是说,直到我出现。
The libraries of shared code I created tied the shoelaces of each foot to each other. Steps by one business domain could not be made without first synchronizing with the other. Maintenance costs in those independent functions used to be negligible, but the common library required an order of magnitude more testing.
我创建的共享代码库将每只脚的鞋带相互捆绑。一个业务域的步骤必须先与另一个业务域同步才能进行。这些独立功能的维护成本过去可以忽略不计,但是公共库却需要增加一个数量级的测试。
While I'd decreased the absolute number of lines of code in the system, I had increased the number of dependencies. The context of these dependencies is critical — had they been localized, it may have been justified and had some positive value. When these dependencies aren't held in check, their tendrils entangle the larger concerns of the system even though the code itself looks just fine.
虽然我减少了系统中代码的绝对行数,但却增加了系统的依赖关系。这些依赖关系是致命的---如果它们是用于本地化,那么它可能是合理的,并具有一些积极的价值。当这些依赖关系没有得到控制时,它们会对整个系统的造成更大问题---尽管代码本身看起来很好。
These mistakes are insidious in that, at their core, they sound like a good idea. When applied in the right context, these techniques are valuable. In the wrong context, they increase cost rather than value. When coming into an existing code base with no knowledge of the context where the various parts will be used, I'm much more careful these days about what is shared.
这些错误是隐蔽而危险的,因为从本质上讲,它们听起来是个好主意。当在正确的环境中应用时,这些技术是有价值的。在错误的环境下,它们增加的是成本而不是价值。现在,当我遇到一个代码库,而不知道各个部分将被使用的上下文时,我对共享的内容要小心得多。
Beware the share. Check your context. Only then, proceed.
谨慎提取共享代码。好好检查上下文。只有这时,继续处理。
Last updated