High-Quality Routines(1)

本文介绍了程序例程的定义,阐述了高质量例程应具备的特征,如命名好、有文档等。还说明了创建例程的理由,包括降低复杂度、避免代码重复等。同时提及了好的例程命名规则和不同类型的内聚,如功能内聚、顺序内聚等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值