← LibraryGuides

Clean Architecture

Robert C. Martin
≈ From memory

Software architecture should be designed to protect the core business logic from the volatile details of external technologies like databases, UIs, and frameworks.

Open Library

In one sentence

The goal of software architecture is to create a system where the "Business Rules" (the reason the software exists) are isolated from the "Infrastructure" (the tools used to run the software).

Why read this (and who it's for)

This book is for software engineers, architects, and technical leads who find themselves in a "Big Ball of Mud"—where a change to a database schema breaks a UI component, or a framework upgrade requires rewriting the core business logic.

It solves the problem of Software Rot: the tendency for systems to become increasingly fragile and difficult to maintain as they grow. After reading this, you will be able to design systems where you can swap out a database, a web framework, or a 3rd-party API without touching a single line of your core business logic.

The big idea

Robert C. Martin (Uncle Bob) argues that the greatest mistake in software engineering is allowing the "details" of the implementation to dictate the shape of the business logic. When we use a specific database (like PostgreSQL) or a specific framework (like Spring or Django), we often let those tools' constraints leak into our core logic. This creates a "coupled" system where the business rules are hostage to the technology choices of the day.

The central thesis is that Dependency Direction must always flow toward the center of the system. The "Core" (Entities and Use Cases) should know nothing about the "Outer" layers (Web, Database, UI, Hardware). By enforcing a strict boundary, you ensure that the software remains maintainable, testable, and independent of the ever-changing technological landscape.

Key ideas

The Dependency Rule

Entities as the Core

Use Cases as Orchestrators

The Database as a Detail

The UI as a Detail

Frameworks & tools

The Dependency Rule Checklist

When designing a new feature, use this checklist to ensure your architecture remains "Clean":

  1. Identify the Entity: What is the "noun" of this feature? (e.g., Order, Invoice).
  2. Identify the Use Case: What is the "verb"? (e.g., PlaceOrder, CancelOrder).
  3. Check the Dependency: Does the Entity know about the Database? (If yes, Refactor).
  4. Check the Use Case: Does the Use Case know about the Web Framework? (If yes, Refactor).
  5. Verify the Flow: Does the flow of dependency always point toward the Entity/Use Case?

The Repository Pattern (Implementation)

To decouple the database from the core logic, follow these steps:

  1. Define an Interface in the Application/Domain layer (e.g., UserRepository).
  2. Implement the Interface in the Infrastructure layer (e.g., SqlUserRepository).
  3. Inject the Implementation into the Use Case at runtime.
  4. Result: The Use Case only knows about the method find_by_id(), not the SQL query.

Memorable examples & stories

The "Big Ball of Mud"

Martin describes a common scenario where a developer adds a "simple" feature to a project, but because the code is so intertwined, they end up breaking five unrelated features. Lesson: Coupling is the silent killer of velocity.

The Database-Centric Design

He describes a system where the "Model" is actually just a wrapper around a database table. When the business logic needs to change, the developer has to perform complex SQL joins and migrations just to change a simple rule. Lesson: If your data structure is the only thing defining your app, you don't have a business logic layer.

The "Switchable" UI

He envisions a system where you could swap a React frontend for a Flutter mobile app by simply changing the "Delivery Mechanism" (the outer layer) while the "Use Case" (the inner layer) remains untouched. Lesson: Architecture is about making the "replaceable" parts easy to replace.

Key quotes

"The database is a detail." Why it matters: This is the hardest pill for many developers to swallow, but it is the foundation of independent architecture.

"The dependency rule is the most important rule in software architecture." Why it matters: It provides a clear, objective test for whether your architecture is "clean" or "dirty."

"Software architecture is about the decisions that are hard to change later." (Paraphrased) Why it matters: It forces you to focus on the structural integrity of the system rather than the superficial aesthetics of the code.

How to apply it

This week

  1. Identify one "Leaky" Dependency: Find a place where a Domain Entity is importing a Database or Web Framework library.
  2. Draw the Dependency Map: Take a piece of your current code and draw the arrows of dependency. If an arrow points from the "Core" to the "Infrastructure," you've found a violation.
  3. Create a Repository Interface: If you have a Use Case that directly calls a database, extract an interface for that database operation.

Ongoing

Common pitfalls

Connects to

If you remember only three things

Internalize it

Q: What constitutes the "Core" in Clean Architecture? A: Entities (Enterprise rules) and Use Cases (Application rules).

Q: Why is the Database considered a "Detail"? A: Because it is a persistence mechanism that can be swapped, changed, or replaced without altering the fundamental rules of the business.

Q: What is the role of a Use Case? A: To orchestrate the flow of data and coordinate the interaction between Entities.

Q: How does Clean Architecture handle the UI? A: The UI is a "Delivery Mechanism" (an outer layer) that captures input and triggers Use Cases.

Q: What is the primary benefit of the Repository Pattern in this context? A: It abstracts the data access logic, allowing the Use Case to remain independent of the specific database technology.

More from Robert C. Martin