Deep Module and Shallow Module Explained and Compared


Module’s Depth

In software design, modulation enables developers to encapsulate complex business logic into manageable blocks of code.

In the book A Philosophy of Software Design, a simple yet powerful heuristic was proposed to evaluate modulation design: depth.

Given a certain amount of logic complexity, a module with narrower interfaces is regarded as Deep Module, while a module with broader interfaces is regarded as Shallow Module.

Deep Module vs Shallow Module

The following diagram illustrates Deep Module and Shallow Module.

stateDiagram-v2
    state DeepModule {
        int1 --> fun1
        fun1 --> fun2
        fun2 --> fun3
    }
    state ShallowModule {
        int2 --> fun4
        int3 --> fun5
        int4 --> fun6
    }

An effective module is generally deep, meaning a simple interface encapsulates relatively larger complexity. An ineffective module is characterized by broader interfaces, encapsulating less complexity per interface than a deep module.

And their major differences can be summarized as follows:

Deep ModuleShallow Module
interfacenarrowerbroader
modular complexityhighlow
inter-modular integration complexitylowhigh

Real-world Strategies

In reality, even module were designed to be perfectly deep, they could become shallower over time, mostly due to incremental business requirements, along with reluctance to incur potential risk on altering existing interfaces.

As a module becomes shallower, the complexity of system integration is likely to rise, resulting in higher maintenance cost. Therefore, it’s wise to evaluate modules’ depth regularly as part of software maintenance.

References