code complete(3)

来源:互联网 发布:乐普数据乐宝宝app 编辑:程序博客网 时间:2024/05/16 18:08

 Design Building Blocks: Heuristics

 

Find Real-World Objects

Form Consistent Abstractions

Good programmers create abstractions at the routine-interface level, class-interface level, and package-interface level—in other words, the doorknob level, door level, and house level—and that supports faster and safer programming. 

 

Information hiding is a particularly powerful heuristic for Software's Primary Technical Imperative because, beginning with its name and throughout its details, it emphasizes hiding complexity.

Get into the habit of asking "What should I hide?" You'll be surprised at how many difficult design issues dissolve before your eyes.

You can add at least two levels of flexibility and readability to your use of status variables:

  • Don't use a boolean variable as a status variable. Use an enumerated type instead. It's common to add a new state to a status variable, and adding a new type to an enumerated type requires a mere recompilation rather than a major revision of every line of code that checks the variable.

  • Use access routines rather than checking the variable directly. By checking the access routine rather than the variable, you allow for the possibility of more sophisticated state detection. For example, if you wanted to check combinations of an error-state variable and a current-function-state variable, it would be easy to do if the test were hidden in a routine and hard to do if it were a complicated test hard-coded throughout the program.

变量的使用是隐藏信息的重要手段

A good technique for identifying areas likely to change is first to identify the minimal subset of the program that might be of use to the user. The subset makes up the core of the system and is unlikely to change. Next, define minimal increments to the system. They can be so small that they seem trivial. As you consider functional changes, be sure also to consider qualitative changes: making the program thread-safe, making it localizable, and so on. These areas of potential improvement constitute potential changes to the system; design these areas using the principles of information hiding. By identifying the core first, you can see which components are really add-ons and then extrapolate and hide improvements from there.先考虑系统核心,很小很小地增加功能,越来越容易变化

 

Keep Coupling Loose像火车箱一节一节容易钩挂

 

In short, the more easily other modules can call a module, the more loosely coupled it is, and that's good because it's more flexible and maintainable. In creating a system structure, break up the program along the lines of minimal interconnectedness.耦合度越低越好,越容易调用越好,调用方式越灵活越好,调用接口的数据类型越简单,参数个数越少越好。

 

Classes and routines are first and foremost intellectual tools for reducing complexity. If they're not making your job simpler, they're not doing their jobs.

 

Look for Common Design Patterns

Patterns reduce complexity by providing ready-made abstractions

Patterns reduce errors by institutionalizing details of common solutions

Using a design pattern is thus conceptually similar to using library code instead of writing your own

Patterns provide heuristic value by suggesting design alternatives 查找一种适合的设计模式比自己创造一种模式更容易,利用别人的经验(被证明是正确的)比自己摸索更容易

Patterns streamline communication by moving the design dialog to a higher level 加速交流

 

The reduced-complexity benefit is that the fewer places you have to look for something, the easier and safer it will be to change.

 

 Diagrams are another powerful heuristic tool. A picture is worth 1000 words—kind of.

原创粉丝点击