The Big Picture: Applying 'Clean Architecture'

TL;DR: "Clean Architecture" is about structuring your system to be independent of frameworks, UI, and databases. The core ideas: 1) The Dependency Rule: Dependencies only point inwards, protecting your business logic. 2) Screaming Architecture: Your top-level structure should declare its purpose, not the tools it uses. 3) Frameworks are Plugins: Treat them as details. The result is a system that's testable, maintainable, and built for the long haul.

If "Clean Code" is about the details of craftsmanship, Robert C. Martin's "Clean Architecture" is about the blueprint of the entire structure. It provides a set of principles for designing software systems that are robust, maintainable, testable, and independent of their external dependencies like databases, frameworks, and the UI.

The Core Concept: The Dependency Rule

The heart of Clean Architecture is the Dependency Rule: source code dependencies can only point inwards.

Imagine a series of concentric circles, like an onion.

  • Innermost Circle: Core business logic and entities.
  • Outer Circles: Application-specific business rules.
  • Outermost Circles: Frameworks, databases, UI, and other external concerns.

The rule means that code in an inner circle cannot know anything about code in an outer circle. Your core business logic should have no idea what database you're using or whether it's being delivered via a web API or a desktop app.

A Relatable Example: Your core logic for calculating a loan amortization schedule should not contain any SQL queries. The database is a detail. The business rules are the core. The database code should depend on (and implement interfaces defined by) the business logic layer, not the other way around. This means you can swap out your database without changing a single line of your precious business rules.

Principle 1: A Screaming Architecture

Your application's top-level directory structure should scream its purpose, not the framework it uses.

When you look at a project and the top-level folders are controllers, views, and models, what does it do? It's a web app, probably using a framework like Rails or Django. But what is its domain?

A clean architecture would have top-level folders like loan-applications, risk-analysis, and customer-notifications. The architecture itself tells you about the business domain, making the system easier to understand for new developers.

Principle 2: Frameworks are Tools, Not Gods

A framework is a detail that should be kept at the outer layers of your onion. Your application should not be a "Spring application" or a "Next.js application"; it should be a Loan Processing application that uses Spring or Next.js.

This principle keeps your core logic independent. By treating the framework as a plugin to your application, you can upgrade it—or even replace it entirely—without a massive, risky rewrite of your business rules. This is the ultimate defense against being locked into technical decisions that may become obsolete.

The Payoff

Following the principles of Clean Architecture requires discipline. It sometimes feels like you're writing more code to create boundaries and abstractions. But the payoff is immense: a system that is independent, testable, and truly adaptable to the one constant in our industry—change.

Ming Zou