the first and most important quality of modular code is encapsulation.
Encapsulated modules don't expose their internals to each other.
They don't call into the middle of each others' implementations, and they don't promiscuously share global data. They communicate using application programming interfaces (APIs) — narrow, well-defined sets of procedure calls and data structures. This is what the Rule of Modularity is about
Write shy code—modules that don't reveal anything unnecessary to other modules and that don't rely on other modules' implementations.
Avoid global data.
Every time your code references global data, it ties itself into the other components that share that data.
Avoid similar functions.
Often you'll come across a set of functions that all look similar—maybe they share common code at the start and end,
but each has a different central algorithm.
Tips
make it easy to reuse 模块之间便于重复利用
There Are No Final Decisions
Eliminate Effects Between Unrelated Things 无关的事情不应该耦合在一起
Use a Single Editor Well 用好一种编辑器:选一种编辑器,彻底了解它,并将其用于所有的编辑任务
Program Close to the Problem domain
Always Use Source Code Control:总是使用源码控制即使团队只有一个人,项目只有一周,确保每样东西都处在源码控制之下
Fix the Problem, Not the Blame 要修正问题,而不是发出指责 bug 是你的过错还是别人的过错,并不是真的很有关系。它仍然是你的问题
Crash Early 早崩溃:尽早检测问题的好处之一是你可以更早崩溃
If It Can’t Happen, Use Assertions to Ensure That It Won’t 如果它不可能发生,用断言确保它不会发生。无论何时你发现自己在思考『但那当然不可能发生』,增加代码检查它,最容易的方法是使用断言
There Are No Final Decisions
Use Exceptions for Exceptional Problems 将异常用于异常的情况。异常表示即时的、非局部的控制转移——这是一种级联的 (cascading) goto。那些把异常用作其正常处理的一部分的程序,将遭受到经典的诶意大利面条式代码的所有可读性和可维护性问题的折磨。这些程序破坏了封装;通过异常处理,例程和它们的调用者被更紧密地耦合在一起
参考资料
简单的几个原则
模块原则
清晰原则
组合原则
简洁原则
吝啬原则
透明性原则
表示原则
通俗原则
优化原则
封装
API
Compactness
Orthogonality
DRY rule
观点
Top-down, bottom-up, and glue layers
Bottom-up programming
Glue layer
程序员修炼之道
type/system #type/code #public