knowledge-base

Modules

Different platforms offer different reuse mechanisms for code, but all support some way of grouping related code together in modules. This concept of modules is proven slippery to define. Therefore, we will use a definition from the Fundamentals of Software Architecture - Chapter 3 book.

Modularity is an organizing principle. IF an architect designs a system without paying attention to how the pieces wire together, a myriad of difficulties might be presented. Good modularity exemplifies the definition of an implicit architectural characteristic. No project features a requirement that asks the architect to ensure good modular distinction and communication, yet sustainable code bases require order and consistency.

Definition

Modularity describes a logical grouping of related code, which could be a group of classes in a OO language or functions in a functional language. This grouping does’t imply a physical separation, merely a logical one.

When it comes to restructuring the architecture, the coupling encouraged by louse partitioning becomes an impediment to breaking the monolith apart. Thus, it is useful to talk about modularity as a concept separate from the physical separation forced or implied by a particular platform. Components are the physical manifestation of modules. Note that component can contain one ore more modules.

Measuring

Given its importance, there are a variety of language-agnostic metrics to help measure modularity. There are 3 key concepts: cohesion, coupling, and connascence.

NOTE!

This measuring techniques come from the structured programming field. Some of it might also apply to OO and Functional paradigms, however, these metrics are originally targeting structured code bases.

Cohesion

Refers to what extent the parts of a module should be contained within the same module. Measuring how relates parts are to each other. Ideally a cohesive module is one where all the parts should be packaged together. Attempting to divide a cohesive module would only result in increased coupling, and decreased readability.

Coupling

A basic measuring method would be by just measuring afferent and efferent coupling:

The following approach allows for a deeper evaluation than the basic afferent/efferent measurements.

Connascence

A refinement of the afferent/efferent measures towards OO.

Two components are connascence if a change in one would require the other to be modified in order to maintain the overall correctness of the system.

Static Connascence

This is analyzed at build/source code level.

Source-code-level coupling, the degree to which something is coupled, either afferent or efferent:

Dynamic Connascence

This is analyzed at runtime.

Connascence Properties

Connascence is an analysis tool for architects and developers, some properties of Connascence help developers use it wisely.

How to use connascence to improve modularity

  1. Minimize overall connascence by breaking the system unto encapsulated elements.
  2. Minimize any remaining connascence that crosses encapsulation boundaries.
  3. Maximize the connascence within encapsulation boundaries.

Conclusions

Resources