img wangjiong

High-Quality Routines(1)

发表于2004/9/22 18:44:00  533人阅读

分类: Excerpt of CC2

What is a "routine"?
A routine is an individual method or procedure invocable for a  single purpose.

What is a high-quality routine?
It's a difficult question because it may include many features such  as:
1. has a good name
2. has good document.
3. have good layout.
4. doesn't read or write the global variables.
5. has a single purpose.
6. defends itself against bad data.
7. doesn't use magic number.
8. has appropriate parameters.

Reason to Create a Routine:
1. Reduce complexity.
2. Make a section of code readable.
Putting a section of code into a well-named routine is one of the best ways to document its purpose.
3. Avoid duplicate code.
4. Hide sequences
It's a good idea to hide the order in which events happen to be processed. WHY???
5. Improve protability.
6. Simplify complicated boolean tests.
Understanding complicated boolean tests in detail is rarely necessary for understanding program flow. Putting such a test into a function makes the code more readable.
7. Improve performance.

One of the strongest mental blocks to creating effective routines is a reluctance to create a simple routine for a simple purpose. Constucting a whole routine to contain two or three lines of code might seem like overkill. But experience shows how helpful a good small routine can be. First, it improves the readable. Second, the small operation tend to turn into larger operations.

Good Routine Names
1. Describe everything the routine does.
2. Avoid meaningless or wishy-washy verbs.
Routine names like HandleCalculation(), PerformServices(), ProcessInput() don't tell you what the routines do.
3. Make names of routines as long as necessary.
Research shows that the optimum average length for a variable name is 9 to 15 characters.
4. To name a function, use a description of the return value
Such as cos(), getColor(), isReady()
5. To name a procedure, use a strong verb followed by an object.
6. Use opposite precisely.
such as add/remove, begin/end, first/last, get/put.

Type of Cohesion
1. Functional cohesion
Is the strongest ad best kind of cohesion, occurring when a routine performs one and only one operation.
2. Sequential cohesion
Exists when a routine contains operations that must be performed in a specific order, that share data from step to step, and that don't make up a complete function when done together.
3. Communicational cohesion
Occurs when operation in a routine make use of the same data and aren't related in any other way.
4. Temporal cohesion
Occurs when operations are combined into a routine because they are all done at the same time. Typical examples would be Startup().

The remaining kinds of cohesion are generally unacceptable.
5. Procedural cohesion
Occurs when operations in a routine are done in a specified order
6. Logical cohesion
Occurs when serveral operations are stuffed into the same rouitne and one of the operations is selected by a control flag that's passed in.
7. Coincidental cohesion
Occurs when the operations in a routine have no discernible relationship to each other.

0 0



取 消