Characteristics of high quality routines

A routine is an individual function or procedure invocable for a single purpose. Example includes a function in C, a function or a procedure in Pascal or Ada, a subprogram in Basic, and a subroutine in Fortran. The name of the routine is an indication of its quality. If the name is bad and it's accurate, the routine might be poorly designed. If the name is bad and it's inaccurate, it's not telling you what the program does. Either way, a bad name means that the program needs to be changed.


Reasons to create

Reasons to create


Good Routine Names


Cohesion

Routine Cohesion

Cohesion means how closely related the operations in a routine are. It can be referred to as "strength", or how strongly your routines are related to each other. The overall goal is to have each routine do one and only one thing, do that one thing well, and not do anything else. The reward is greater reliablity of your code, and your code is easier to modify when anticipating future changes to the requirements of the program. The following types of cohesion are deemed acceptable types of cohesion.


Unacceptable Cohesion

Unacceptable Cohesion

This occurs when code is pooly organized, hard to debug, and hard to modify. If a routine has bad cohesion, it's better to rewrite it than to try to fix the problem.


Intimacy

Intimacy

This refers to the directness of the connection between two routines.


Visibility

Visibility

Visibility refers how much the transparency of the connection between two routines. The connection should be as obvious as possible. The visibility can be achieved through different ways. One of them is to pass data through parameter list. The method makes the connection obvious. One bad example is to modify global data to ensure another routines to use it. Documenting the global-data connection makes it more obvious and is slightly better.


Flexibility

Flexibility

Flexibility refers to how easily you can change the connections between routines.Ideally, you want something more like the snap-in modular connector on your phone than like bare wire and a soldering gun. Fiexibility is partly a product of the other coupling characteristics, but it's a little different too. In short, the more esaily other routines can call a routine, 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. If a program were a piece of wood, you would try to split it with the grain.


How Big Should It Be

How Big Should It Be


Defensive Programming

Defensive Programming


Examples of Bad and Good Routine

Examples of Bad and Good Routines


Reference:
Code Complete, A Practical Handbook of Software Construction.
Steve McConnell. Microsoft Press (1993).


Original Authors :Brien Beattie, Dean Varg, Yixiang Wang, Yawen Wu, Hong Zhang
 Date : June 4, 2000 
 Modififed By :Sirigon Sukpan, Hua Ma, Cyren Aldecoa, Gerald Barrie, Curtis Ferchoff,Quanxiang Li  
 Date : June 8, 2000
 Copyright 2000 Department of Computer Science, University of Regina. 

Index
Next


[CS Dept Home Page]