Coupling (computer science)

From Wikipedia, the free encyclopedia

In computer science, coupling or dependency is the degree to which each program module relies on each one of the other modules.

Coupling is usually contrasted with cohesion. Low coupling often correlates with high cohesion, and vice versa. The software quality metrics of coupling and cohesion were invented by Larry Constantine, original developer of Structured Design (see also SSADM).

Contents

[edit] Low coupling

Coupling can be "low" (also "loose" and "weak") or "high" (also "tight" and "strong"). Low coupling refers to a relationship in which one module interacts with another module through a stable interface and does not need to be concerned with the other module's internal implementation (see Information Hiding). With low coupling, a change in one module will not require a change in the implementation of another module. Low coupling is often a sign of a well-structured computer system, and when combined with high cohesion, supports the general goals of high readability and maintainability.

Systems that do not exhibit low coupling might experience the following developmental difficulties:

  • Change in one module force a ripple of changes in other module.
  • Modules are difficult to understand in isolation.
  • Modules are difficult to reuse or test because dependent modules must be included.

The concept of coupling is usually related to the concept of cohesion: low coupling facilitates high cohesion, and vice versa. For example, one approach to increasing cohesion is functional design, which seeks to limit the responsibilities of modules along functionally-related boundaries. Modules with single responsibilities tend to communicate less with other modules, which typically causes the side-effect of reduced coupling. This can also be seen in object-oriented programming, where coherence is said to increase when classes are refactored to contain more closely-related code. This tends to cause the connections between the classes to become less dependent on their internal implementations, which results in reduced coupling. Specifically, coupling increases between two classes A and B if:

  • A has an attribute that refers to (is of type) B.
  • A calls on services of an object B.
  • A has a method which references B (via return type or parameter).
  • A is a subclass of (or implements) class B.

Low coupling may also reduce performance, and a highly-coupled system is sometimes desirable to achieve maximum efficiency. Regardless, in many modern computing systems, the cost of reduced performance is often seen as a worthy trade for the benefits to the software development process that result from low coupling.[citation needed]

[edit] Types of coupling

The types of coupling, in order of lowest to highest coupling, are as follows:

Message coupling (low and best)
This is the loosest type of coupling. Modules are not dependent on each other, instead they use a public interface to exchange messages.
Data coupling
Data coupling is when modules share data through, for example, parameters. Each datum is an elementary piece, and these are the only data which are shared (e.g. passing an integer to a function which computes a square root).
Stamp coupling (Data-structured coupling)
Stamp coupling is when modules share a composite data structure and use only a part of it, possibly a different part (e.g. passing a whole record to a function which only needs one field of it).
This may lead to changing the way a module reads a record because a field which the module doesn't need has been modified.
Control coupling
Control coupling is one module controlling the logic of another, by passing it information on what to do (e.g. passing a what-to-do flag).
External coupling
External coupling occurs when two modules share an externally imposed data format, communication protocol, or device interface.
Common coupling
Common coupling is when two modules share the same global data (e.g. a global variable).
Changing the shared resource implies changing all the modules using it.
Content coupling (worst)
Content coupling is when one module modifies or relies on the internal workings of another module (e.g. accessing local data of another module).
Therefore changing the way the second module produces data (location, type, timing) will lead to changing the dependant module.

In object-oriented programming, subclass coupling describes the relationship between a class and its parent. The class is connected to its parent, but the parent isn't connected to the child.

[edit] Known uses

Dependency is also common in talking about software package management. One software package, in order to work or to be fully functional, may depend on other software packages and thus must also be installed, and their specific versions must be known if backward compatibility is broken between versions. The Advanced Packaging Tool package format, some versions of the Red Hat Package Manager package format, and Portage include dependency information between packages. This is convenient for updating software but can lead to dependency hell.

Build systems like make are also dependency driven in the sense that a more complex object, like a program, only gets linked together once all its dependencies, i.e. the objects it is comprised of, have been compiled.

High cohesion and low coupling are attributes of good design.


[edit] See also

[edit] External links

In other languages