Architecture 2025-06-25

Cohesion and Coupling

Understand cohesion and coupling as software design metrics. Learn the 7 levels of cohesion (coincidental to functional) and 6 levels of coupling (content to data) with practical examples.

Read in: ja
Cohesion and Coupling

Overview

In software design, Cohesion and Coupling are fundamental metrics for measuring quality. These concepts are crucial when considering modularization, maintainability, and reusability.

What is Cohesion?

Cohesion is a metric that represents how closely related the components (such as functions and variables) within a module are.

High Cohesion

Low Cohesion

Types of Cohesion (Low → High)

Type Description
Coincidental Unrelated processes are just grouped together
Logical Processes of the same category, selected by conditions
Temporal Processes executed at the same time (e.g., initialization)
Procedural A series of processes with different purposes
Communicational Processes that handle the same data
Sequential Output becomes the input for the next process
Functional Specialized for a single, clear purpose (ideal)

What is Coupling?

Coupling is a metric that represents the strength of dependencies between modules.

Low Coupling

High Coupling

Types of Coupling (High → Low)

Type Description
Content Directly accessing the internals of another module
Common Sharing global variables
External Depending on external formats (such as file formats)
Control Delegating control via flags
Stamp Passing structures containing unnecessary data
Data Passing only the minimal necessary data (desirable)
Message Complete separation through message passing (ideal)

Ideal

Metric Ideal State
Cohesion The higher, the better (focused on purpose)
Coupling The lower, the better (less dependency)

Example: Designing a Logging Feature

Good Example: High Cohesion & Low Coupling

type Logger struct {
    Output io.Writer
}

func (l *Logger) Info(msg string)  { ... }
func (l *Logger) Error(msg string) { ... }

Bad Example: Low Cohesion & High Coupling

func DoStuffAndLog() {
    // Data processing
    // DB update
    // Send email on error
    // Log output
}

Summary

Cohesion and coupling are crucial criteria that influence design quality. By clarifying module responsibilities and minimizing dependencies, a system design that is easy to understand, maintain, and scale can be achieved.

Tags: Design System Design
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles