DomainDrivenDesign.ir | Clean Code Mastery 2026

Logo

Clean Code Mastery workshop

View the Project on GitHub DomainDrivenDesignSchool/CleanCodeMastery2026

Design by Contract (DbC)

Clear expectations => explicit guarantees => safer change

Design by Contract is about making implicit assumptions explicit.

Code should not hope to be used correctly.
It should define the rules of engagement.


Home

Related Docs


What Is Design by Contract?

Design by Contract (DbC) is a design approach where software elements clearly state:

Every interaction becomes a contract, not a guess.


The Three Pillars of a Contract

1️⃣ Preconditions (What I Expect)

Conditions that must be true before a method is called.

Examples:

❌ If preconditions are violated => the caller is wrong


2️⃣ Postconditions (What I Guarantee)

Conditions that will be true after the method finishes successfully.

Examples:

❌ If postconditions are violated => the method is wrong


3️⃣ Invariants (What Always Holds)

Conditions that must always be true for an object or module.

Examples:

Invariants define identity and consistency


Design by Contract & Clean Code

Clean Code is not about beauty - it is about trust.

Design by Contract makes that trust explicit.

Clean Code Principle Contract Connection
Readability Rules are visible, not hidden
Predictability Behavior is guaranteed
Simplicity Fewer defensive checks everywhere
Intentionality Code says why, not just how

Clean code reads like a promise - contracts make it enforceable.


Design by Contract & Code Smells

Many code smells are actually broken or missing contracts.

A smell often says: “I don’t know how this is supposed to be used.”


🔧 Design by Contract & Refactoring

Refactoring without contracts is risky.
Contracts turn refactoring into a safe operation.

Before Refactoring

During Refactoring

After Refactoring

Refactor behavior inside the contract, not the contract itself.


🧬 Design by Contract in the Biological Metaphor

Software Level Biological Metaphor Contract Role
Field / Property Atom Value constraints
Method Molecule Preconditions & postconditions
Class / Service Cell Invariants
Module / Layer Tissue Interaction contracts
System Organism Global consistency rules

Contracts prevent local mutations from killing the organism.


🧪 Contracts ≠ Tests (But They Work Together)

Contracts Tests
Define expectations Verify behavior
Executed at runtime Executed in test phase
Fail fast Detect regressions
Guard boundaries Validate scenarios

Contracts define what must never happen.
Tests explore what should happen.


⚠️ Common Myths

❌ “Design by Contract is just asserts”
✅ Asserts are tools - contracts are design decisions

❌ “Contracts slow development”
✅ They reduce debugging, rework, and fear of change

❌ “We have tests, we don’t need contracts”
✅ Tests don’t protect runtime misuse


🧭 Practical Guideline

The more critical the code, the stronger the contract.


Origin & Philosophy of Design by Contract

Design by Contract was introduced by Bertrand Meyer,the creator of the Eiffel programming language, in his seminal book:

Object-Oriented Software Construction

Meyer’s core idea was simple but radical:

Correct software is not accidental - it is designed through explicit agreements.

In Eiffel, contracts were language-level features, not comments or conventions:

This matters because Design by Contract was never meant to be a testing trick.It was designed as a language-driven design discipline.

Design by Contract in Domain-Driven Design (DDD)

Design by Contract fits naturally into Domain-Driven Design.

In DDD, contracts are not technical checks - they are domain rules.

How DbC Maps to DDD Concepts

DDD ConceptContract RoleValue ObjectConstructor enforces validityEntityInvariants preserve identityAggregate RootGatekeeper of all contractsDomain ServiceExplicit behavioral expectationsBounded ContextContract boundaryUbiquitous LanguageContracts are executable language

In DDD, invariants are business rules, not implementation details.

Example:

“An order total can never be negative”This is not a test.This is a domain invariant.

🗣 Design by Contract & Language-Driven Design (LDD)

Design by Contract is a natural extension of LDD.

Contracts are where language becomes enforceable behavior.

Without contracts:

⚙️ Clean Code vs Design by Contract (Not Opposites)

Clean Code (popularized by Robert C. Martin) focuses on:

Design by Contract focuses on:

They are complementary:

Clean Code makes intent readableDesign by Contract makes intent enforceable

⚠️ Real-World Challenges of Design by Contract

Let’s be honest - DbC is powerful, but not free.

1️⃣ Lack of Native Language Support

Most mainstream languages do not support DbC natively.

LanguageContract SupportEiffelNativeJava / C#Convention-basedJavaScriptRuntime-onlyGoIdiomatic resistanceRustStrong types, weak contractsPythonDynamic, fragile

Result:

Contracts become culture, not compiler-enforced rules.

2️⃣ Performance & Runtime Cost

This leads to:

“We’ll add contracts later.”

Later rarely comes.

3️⃣ Where Should Contracts Live?

Common mistakes:

Better approach:

Trust inside - verify at the edges.

🧭 Practical, Modern DbC Guideline

A realistic DbC strategy today:

Contracts should be boring, predictable, and loud when broken.

🔑 Why DbC Belongs in This Repository

Including Design by Contract here signals that:

This repository is not just about prettier code —it’s about designing trust into software.


Final Thought

Code is a conversation.
Contracts make sure everyone speaks the same language.