【笔记】读卡马克《Parallel Implementations》

在浏览知乎的一个讨论: 《入职后发现项目组代码异常混乱,是去是留?》, 顾露同学的回答中提到了这篇文章,所以就追根溯源去读了一下. 因为原博客《AltDevBlogADay》已经关闭了, 可以从这里看到全文。

摘抄文中比较有意思的几段:

>

I used to Code Fearlessly all the time, tearing up everything whenever I had a thought about a better way of doing something. There was even a bit of pride there — “I’m not afraid to suffer consequences in the quest to Do The Right Thing!” Of course, to be honest, the consequences usually fell on a more junior programmer who had to deal with an irate developer that had something unexpectedly stop working when I tore up the code to make it “better”.

卡马克一贯的态度: "Code Fearlessly", "Do The Right Thing", 并且不惧承担随之(重构、甚至重写)而来的风险

>

What I try to do nowadays is to implement new ideas in parallel with the old ones, rather than mutating the existing code. This allows easy and honest comparison between them, and makes it trivial to go back to the old reliable path when the spiffy new one starts showing flaws. The difference between changing a console variable to get a different behavior versus running an old exe, let alone reverting code changes and rebuilding, is significant.

卡马克的方法论:并行实现重构/重写的新代码。如果站在服务器的角度衍生来看,新/老接口同时发布(灰度发布), 线上采集真实数据做对比验证, 出了问题回滚也容易,直接替换接口。 有兴趣的可以读下《什么是灰度发布?》

当然, 这里有个默认前置: 是要有合理的软工设计, 接口清晰,足够SRP和ISP。

>

If the task you are working on can be expressed as a pure function that simply processes input parameters into a return structure, it is easy to switch it out for different implementations. If it is a system that maintains internal state or has multiple entry points, you have to be a bit more careful about switching it in and out. If it is a gnarly mess with lots of internal callouts to other systems to maintain parallel state changes, then you have some cleanup to do before trying a parallel implementation.

如果是一个内部状态复杂的系统/服务, 或者与其他的系统有各种调用依赖, 那么,首先得把服务本身梳理清楚,才能继续开搞。这里反面说明了上面提到的默认前置。

系统为什么会复杂? 大部分都可以归结为四个字:“历史原因”, 原始设计跟不上需求变化时就会出现。所以, 在工程敏捷迭代的同时, 一方面不要吝啬给代码重构花时间, 另一方面,还需要Coder坚持正向的价值观(BGM: 再次响起卡马克的原话,"Code Fearlessly", "Do the Right Thing")

>

There are two general classes of parallel implementations I work with: The reference implementation, which is much smaller and simpler, but will be maintained continuously, and the experimental implementation, where you expect one version to “win” and consign the other implementation to source control in a couple weeks after you have some confidence that it is both fully functional and a real improvement.

这段是卡马克关于实现 Parallel Implementations 的具体方法阐述, 类似的方法和手段在《重构》这本书中讲的比较多.

读者结语:卡马克推荐的 Parallel Implementations 作为代码重构的方式, 在软件工程中是相当有效的, 在 Parallel 的阶段可以充分验证可行性,减少大部分风险,值得学习和使用。