|
| |
Modularization (Packages)
A module or package in UML is a collection of objects that are logically
related. Those objects may include constants, data types, variables, and program
units (e.g., functions, procedures, etc.). Note that objects in a module need
not be physically related. For example, it is quite possible to construct a
module using several different source files. Likewise, it is quite possible to
have several different modules in the same source file. However, the best
modules are physically related as well as logically related; that is, all the
objects associated with a module exist in a single source file (or directory if
the source file would be too large) and nothing else is present.
Modules contain several different objects including constants, types, variables,
and program units (routines). Modules shares many of the attributes with
routines; this is not surprising since routines are the major component of a
typical module. However, modules have some additional attributes of their own.
The following sections describe the attributes of a well-written module.
Module Attributes
A module is a generic term that describes a set of program related objects
(routines as well as data and type objects) that are somehow coupled. Good
modules share many of the same attributes as good routines as well as the
ability to hide certain details from code outside the module.
Good modules exhibit strong cohesion. That is, a module should offer a (small)
group of services that are logically related. For example, a "printer"
module might provide all the services one would expect from a printer. The
individual routines within the module would provide the individual services.
Good modules exhibit loose coupling. That is, there are only a few, well-defined
(visible) interfaces between the module and the outside world. Most data is
private, accessible only through accessor functions (see information hiding
below). Furthermore, the interface should be flexible.
Good modules exhibit information hiding. Code outside the module should only
have access to the module through a small set of public routines. All data
should be private to that module.
2.2.2 - Physical Organization of Modules
Many languages provide direct support for modules (e.g., packages). Some
languages provide only indirect support for modules (e.g., a source file in
C/C++). Others, like BASIC, don't really support modules, so you would have to
simulate them by physically grouping objects together and exercising some
discipline.
Insofar as the particular language you're using supports the concept of a
module, embrace that implementation. Beyond that, here are a few rules that can
help make modules easier to read and understand.
- Rule:
- Each module should completely reside in a single source file. If size
considerations prevent this, then all the source files for a given module
should reside in a subdirectory specifically designated for that module.
- Rule:
- If a particular language processing system does not support modules of any
kind, simulate those modules by physically grouping the objects in the
source code. Be sure to access the module using only "approved"
interfaces. Always check for inconsistencies when reviewing your code.
Some people have the crazy idea that modularization means putting each
function in a separate source file. Such physical modularization generally
impairs the readability of a program more than it helps. Strive instead for
logical modularization, that is, defining a module by its actions rather than by
source code syntax (e.g., separating out functions).
This document does not address the decomposition of a problem into its modular
components. Presumably, you can already handle that part of the task. There are
a wide variety of texts on this subject if you feel week in this area.
|