Measuring Coupling Degree

Assume class A references class B:

We would like to measure the strength of dependency d:

couplingDegree(A, B) = ?

This measure should reflect how sensitive A is to changes in B. Obviously, the less sensitive the better. In other words, the lower the coupling degree, the better. The coupling degree depends on three factors.

Stability

The first factor is the stability of B. How likely is a change to B? [Jones] identifies four levels of stability/instability (hence reusability):

1. Foundational classes
2. Architectural classes
3. Domain-specific classes
4. Application-specific classes

The term "domain" refers to the application domain. This is the real world context of the application. Health care, business, manufacturing, and engineering are broad examples of application domains. A domain-specific object represents an object in the application domain. For example, a customer object in a business application would represent a real customer in the application domain. Thus, we can think of a customer object as a virtual or simulated customer. A well designed customer object might be reused or shared by other applications in the business domain, but it seems unlikely that it would be useful in a domain such as scientific measurement.

Examples of foundational components include quantities, strings, dates, and containers. A quantity is a number together with a unit, such as "3.5 US dollars", or "3.5 kilometers". A container is a data structure that holds other objects. Stacks, queues, lists, and arrays are common examples. It's easy to imagine that a well designed stack would be useful in a wide variety of applications cutting across architectures and domains.

Examples of application-specific objects include commands, command processors, and view windows. It's easy to imagine that while two applications in the business domain might share the same customer objects, it seems less likely that these applications would display the customer in the same way or respond to the same user commands in the same way.

Examples of architectural components include message dispatchers and remote server proxies. For example, a well designed database proxy would be useful in any application that needs to encapsulate access to a remote database.

Mixed Domain Coupling

Typically, a class at level n depends on other classes at level m for m <= n, but not classes at level m for n < m. In other words, a class C is only as stable as the least stable class in dependencies(C). Classes that violate this principle are called mixed domain classes:

class Account {
�� View accountView; // app-specific
�� ...
}

class Real {
�� Angle arctan() { ... }
�� Centigrade convert() { ... }
�� USDollars convert() { ... }
}

Access Type

The second factor is the access type:

access(A, B)
�� = 5 if A references a private member of B
�� = 4 if A references a protected member of B
�� = 3 if A references a public field of B
�� = 2 if A calls a public method of B
�� = 1 if A only calls public methods of B
�������� declared in some interface that B implements

Scope of Reference

Is the reference to B a field, parameter, of local variable in A? Obviously the scope of a field is the entire class, while the scope of a local variable is only a block. These scopes affect the coupling degree, too:

scope(A, B)
�� = 3 if B is visible throughout A
�� = 2 if B is visible within a method of A
�� = 1 if B is visible within a block of A
�����

The overall coupling degree is simply the fraction of the maximum possible coupling degree:

couplingDegree(A, B) =
�� (instability(B) + access(A, B) + scope(A, B))/12

Of course we want the coupling degree to be 0 if A does not reference B.

Note: We can modify the coupling degree to be a weighted sum if we want to distinguish between the importance of the various factors.

The coupling degree of a chain of references running from A to B is simply the product of the coupling degrees of each reference in the chain. If A depends on B, then the coupling degree is the maximum coupling degree taken over all chains that run from A to B. The coupling degree of a package or system is simply the average coupling degree of each reference within the system.