You would not board a flight with a ticket that says “probably Madrid, probably tomorrow, probably your seat.” You would not sign a mortgage where the bank’s obligations live in a hallway conversation. You would not accept a job offer where the salary depends on how payroll interprets your name next month.
Yet software is full of agreements like that. A function receives a value that is supposed to be a valid identifier. A method receives a number that is supposed to be positive. A module exposes an operation that only works when the object is in a certain state, but the public interface does not say so. The code compiles, the tests pass, the deployment goes out, and production breaks. The team calls it an edge case.
Many times, it was not an edge case. It was a hidden clause.
Design by Contract (DbC), introduced by Bertrand Meyer through Eiffel and his wider method of object-oriented software construction, gives us a discipline for making those clauses explicit. A software element should say what it expects, what it promises, and what must remain true while it works. Meyer frames those ideas through assertions: preconditions, postconditions, and invariants. The deeper lesson is not syntax. It is responsibility.
This article is language-agnostic. The examples use Kotlin only because Kotlin gives us concise tools for domain-specific types, sealed outcomes, exhaustive when expressions, and explicit argument or state checks. The same thinking applies in Java, C#, TypeScript, PHP, Python, Rust, Swift, Go, Scala, and many other languages.
A contract is not a guard clause with better manners. A contract is the public shape of responsibility.
TL;DR
Design by Contract (DbC) is a way of designing software collaboration through explicit obligations, benefits, and semantic promises.
A contract answers three questions:
Precondition: what must be true before an operation is called?
Postcondition: what must be true after an operation completes successfully?
Invariant: what must remain true for an object, module, aggregate, or abstraction across its observable lifetime?
Meyer’s method treats contracts as part of a broader reliability discipline. They connect classes, abstract data types, inheritance, assertions, exception handling, documentation, and testing. They are not just runtime checks.
Modern languages express contract thinking through types, constructors, factories, assertions, result types, sealed unions, encapsulation, contract tests, boundary parsers, and behavioral interface rules. The mechanism changes. The responsibility does not.
The most useful distinction is this:
Invalid external input is normal and should be translated at the boundary.
Expected business rejection is part of the domain conversation.
Broken internal assumptions are contract violations.
If every unhappy path becomes an exception, the contract is not precise enough.
Table of contents
Hidden clauses are still clauses
A method named reserveSeats receives an identifier and a number.
reserveSeats(flightId, seats)That signature looks harmless. It hides a room full of questions.
What makes a flight identifier valid?
Can
seatsbe zero?Can
seatsbe negative?Is there a maximum number of seats per reservation?
What happens when the flight does not exist?
What happens when the flight exists but has no capacity?
Does the operation mutate state?
Does it return a reservation?
What must be true after it succeeds?
What must remain true even when it rejects the request?
Those facts often live in different places: tests, old tickets, controller validations, database constraints, issue comments, monitoring alerts, and the memory of the developer who first wrote the feature. The operation has rules, but the rules are not part of the operation’s visible agreement.
That is the hidden clause problem. A hidden contract is still a contract. It is just a contract the code cannot help you enforce, explain, or test directly.
Design by Contract is not mainly about adding more checks. It is about moving the agreement from rumor to code.
Meyer’s bigger idea
A shallow explanation of DbC starts with syntax. It says: use require, ensure, and invariants.
That is not wrong, but it is too small. Meyer’s argument is larger than a syntax feature. In Object-Oriented Software Construction, object technology is presented through connected ideas: class as structure and type, contract as reliability discipline, abstract data type as semantic foundation, and inheritance as classification and reuse.
The map matters because it prevents a weak interpretation of DbC.
A class gives software a shape.
A contract gives that shape obligations.
An abstract data type gives those obligations meaning.
Inheritance must preserve that meaning when behavior is reused or specialized.
This is the article’s compass.
Without that compass, DbC becomes “validation with fancy names.” With it, contracts become part of how we design, specify, document, test, reuse, and evolve software.
Meyer also separates the method from the notation. Eiffel made contracts executable and visible in a direct way, but the idea is not trapped inside Eiffel. A Kotlin value object, a Rust newtype, a TypeScript parser, a C# record, a Java sealed hierarchy, a PHP readonly object, or a Python dataclass can all carry pieces of contract thinking when used deliberately.
The question is not, “Does my language have Eiffel syntax?” The question is, “Who owes what to whom, and can the reader see it?”
Correctness needs a specification
Software correctness is not a feeling.
A program is not correct because it compiles. It is not correct because it has tests. It is not correct because the team likes the design. It is correct relative to a specification.
This idea predates DbC. C. A. R. Hoare’s work on axiomatic programming gave us a way to reason about programs through assertions before and after execution. A simplified form is commonly written like this:
{P} A {Q}
Read it as: if operation A starts in a state where P is true, then after A completes successfully, Q should be true.
In that framing:
Pis the precondition.Qis the postcondition.The operation is judged by the relationship between them.
Meyer adapts this style of reasoning into object-oriented design. The everyday lesson is not that every codebase must become a formal proof system. The lesson is that an operation without a specification can only be judged by accident.
A method name tells us what the author hoped the operation would mean. A contract tells us what the operation is allowed to mean.
Assertions put the what beside the how
A program normally tells us how something is done.
It assigns values, calls functions, sends messages, persists data, transforms input into output, and changes state. The implementation is the how.
A contract adds the what. It says what must be true before the work starts, what must be true after it finishes, and what must remain true around the operation. Assertions place part of the specification next to the implementation.
That placement is practical. If the specification only lives in a requirements document, it can drift away from the code. If it only lives in tests, it may cover examples without exposing the full agreement. If it only lives in someone’s memory, it disappears when that person leaves. If it lives in the software text, readers can see the operation and its obligations together.
Assertions serve several jobs:
They guide implementation.
They document behavior close to the code.
They support testing and debugging by making violations visible.
They make exception handling more precise because the system can distinguish broken assumptions from normal outcomes.
They make reuse safer because operating conditions travel with the reusable element.
The word assertion can sound like a runtime mechanism. The deeper idea is methodological. A runtime assertion may be enabled or disabled. The contract remains part of the design.
A class is a small language
Imagine a stack with these operations:
put
remove
item
empty
countThe list is not enough.
Without semantics, put could insert at the top, insert at the bottom, ignore the value, duplicate it, or remove something else first. remove could remove the newest item, the oldest item, or a random item. item could return the top item, the bottom item, or the last inspected item.
Names suggest meaning. They do not prove it.
This is where Meyer’s connection between abstract data types and assertions becomes essential. An abstract data type is not just a name and a list of functions. It includes semantic properties: axioms, preconditions, and relationships between operations.
For a stack, a meaningful contract might say:
A new stack is empty.
After putting an item on a stack that can accept it,
itemreturns that item.After putting an item,
countincreases by one.removeis only valid when the stack is not empty, unless the operation deliberately models empty removal as a normal outcome.Representation details are private.
Two stack implementations may differ completely. One may use an array and an index. Another may use linked nodes. The client should not care. The client cares that the observable behavior satisfies the same contract.
A class is a small language for a concept. The contract is its grammar.
Preconditions, postconditions, and invariants
The three core contract elements are simple, but they become powerful when responsibility is assigned carefully.
A precondition states what must be true before an operation is called. It binds the client. If the precondition is violated, the call is invalid.
A postcondition states what must be true after an operation completes successfully. It binds the supplier. If the postcondition is violated, the supplier failed.
An invariant states what must remain true for the object, module, aggregate, or abstraction across its observable states. It binds the implementation. If the invariant is violated after creation or after an exported operation, the supplier has exposed an inconsistent abstraction.
The relationship is easier to see as a responsibility flow.
For a flight reservation model:
A seat request must represent a valid reservation size.
Available seats must never be negative.
If a reservation is accepted, availability decreases by exactly the requested number of seats.
If there are not enough seats, the request is rejected as a domain outcome.
The last point matters. “Not enough seats” is not automatically a broken contract. In a real reservation system, capacity can change between reading availability and requesting seats. A customer asking for seats on a full flight has not necessarily misused the program. The business simply says no.
A precise contract distinguishes three things:
Invalid call.
Valid call with rejected business outcome.
Supplier failure.
Design improves when those three things stop sharing the same bucket.
Every obligation is also a benefit
Meyer’s contract metaphor works because obligations and benefits are symmetrical.
The client’s obligation is the supplier’s benefit. If the client satisfies the precondition, the supplier can write simpler code because it may assume the condition holds. The supplier’s obligation is the client’s benefit. If the supplier satisfies the postcondition, the client can rely on the promised result without inspecting private details.
That symmetry changes code.
Without a contract, every collaborator becomes suspicious. Every layer checks everything. Every method half-trusts its inputs. Every call is surrounded by nervous code.
With a contract, we can trust the right things.
Not everything. The right things.
A contract is like a bridge with a weight limit. The limit is not there to annoy the driver. It is there so the bridge can be trusted. Once the driver respects the limit, the bridge must do its job. Once the bridge promises to carry the load, the driver does not need to inspect the steel beams before crossing.
Software has the same deal. Once the boundary has turned an external number into a valid seat request, the domain does not need to wonder whether the request is zero. The type, constructor, parser, or validation layer has already made that impossible. Once an availability object exists, the rest of the model does not need to wonder whether it represents negative seats. The invariant should have rejected that state before the object became observable.
A good contract does not add noise. It removes questions.
More reliability by checking less
The phrase “checking less” sounds dangerous until we define it.
It does not mean “trust everything.” It means “do not check the same responsibility everywhere.”
A common defensive style says:
Validate at the controller.
Validate again in the service.
Validate again in the domain object.
Validate again in the repository.
Validate again in the mapper.
Add one more check because you never know.
This looks safe at first. Later, one layer changes a rule and another layer does not. The controller allows ten seats, the domain allows nine, the Application Programming Interface (API) documentation says eight, and the database constraint says something else. The checks were supposed to increase safety. They created drift.
DbC asks a sharper question: who owns this condition?
There are several possible answers:
The boundary owns hostile input translation.
The caller owns the preconditions of valid internal calls.
The supplier owns postconditions.
The object or module owns invariants.
The application owns workflow decisions.
The infrastructure owns external failure handling.
The code becomes simpler when those answers are explicit. Defensive programming often treats every collaborator as suspicious. Contract thinking assigns responsibility so collaborators can trust the right agreement at the right boundary.
Invariants protect abstractions across time
A precondition or postcondition belongs to a particular operation. An invariant belongs to the abstraction as a whole.
Creation must establish the invariant. Exported operations must preserve it when called in a state satisfying their preconditions and the invariant. If inheritance is involved, inherited invariants also constrain descendants. This is one reason contracts and inheritance cannot be separated cleanly.
This matters because invariants are often misunderstood. An invariant does not necessarily mean every private instruction must preserve the object at every micro-step. A mutable object may temporarily move through an intermediate state while an operation is executing. The discipline is that the object must not expose that broken state through its public boundary. It must be whole when creation finishes, and it must be whole again when exported operations return.
Modern systems stretch this idea beyond classes.
An aggregate has invariants.
A module has invariants.
A database transaction may protect invariants.
A distributed workflow may try to preserve invariants through messages, compensations, and eventual consistency.
A type can make some invalid states impossible, although not every business invariant fits inside a type system.
The invariant is the abstraction’s spine. If outside code can bend it, the contract is weaker than it looks.
Contracts inside algorithms
Most practical explanations of DbC stop at preconditions, postconditions, and class invariants. Meyer’s treatment goes deeper. Assertions also help inside algorithms.
A loop invariant states what remains true before and after every loop iteration. A loop variant is a value that moves in a direction that proves the loop is making progress toward termination.
These ideas matter when correctness depends on non-trivial iteration:
Binary search.
Parsers.
Schedulers.
Pricing algorithms.
Reconciliation jobs.
Migration scripts.
Allocation algorithms.
Financial calculations.
Data structure operations.
Consider binary search in plain language. Before each iteration, the target, if it exists, must still be inside the current search interval. Every step must shrink that interval. The invariant protects the meaning of the interval. The variant, often the interval size, argues that the loop cannot continue forever if the algorithm is correct.
If the interval does not shrink, the algorithm may loop forever. If the invariant is broken, the algorithm may skip the target.
Modern teams do not need to write formal proofs for every loop. They should recognize when a loop carries business-critical meaning. In those places, naming the invariant can prevent subtle bugs.
For simple loops, tests may be enough. For complex loops, the invariant is the flashlight.
Demanding contracts and tolerant contracts
A strict contract is not always the best contract. Two styles are useful.
Demanding contract
The operation says: “You may only call me when this condition holds.”
For example:
reserveSeats may only be called when enough seats are available.If the caller violates that condition, the caller is wrong. This style works well when the condition truly belongs to the caller, especially inside a trusted domain model or between internal collaborators.
Tolerant contract
The operation says: “You may call me with a valid request. I will return one of the known outcomes.”
For example:
reserveSeats may return Accepted or Rejected.NotEnoughSeats.This style works well when the condition is part of the normal business conversation. In a reservation system, capacity can change between checking availability and asking for seats. A customer request may be valid even when the business refuses it.
Modern languages give us better tools than raw error codes:
Sealed results.
Discriminated unions.
Result objects.
Sum types.
Error values with typed payloads.
Domain events.
Explicit rejection types.
The contract becomes:
This request is well-formed.
The supplier will attempt the business operation.
The result will be one of the known outcomes.
Each outcome has its own promise.
That is not weaker than throwing exceptions. It is more precise.
Command-query separation
Contracts are easier to understand when commands and queries do not pretend to be the same thing.
A command asks the system to do something. It may change state.
A query asks the system for information. It should not change the abstract state observable by clients.
Meyer’s command-query separation principle prohibits abstract side effects in functions. A concrete internal change may be acceptable if it does not change what clients can observe through public queries. A query may update a cache, lazily compute a value, or reorganize internal storage. If none of that changes the abstract answer to visible queries, the abstraction remains the same.
A query that changes the answer to another visible query breaks the mental model. Asking a question should not change the answer.
This matters because postconditions should usually be observable. A postcondition such as:
After reserving 2 seats from 3 available seats, available seats is 1.should not require looking into private fields. The client should observe it through a public query, returned state, event, or result.
This also improves tests. A behavior-focused test asks, “What can the client observe after the operation?” It does not ask, “Which private collection did the object mutate?”
The test is a witness, not a spy.
Boundary validation is not domain contracting
The outside world does not speak your domain language.
HTTP gives you strings and numbers. JSON does not know what a reservation is. A message broker may deliver stale payloads. A database row from an old version may not satisfy today’s model. A third-party API may lie without malice.
The boundary is not a trusted client. The boundary is weather.
The boundary must parse, validate, and translate before the domain receives anything meaningful.
Many systems lose clarity by treating malformed external input, missing application data, expected domain rejection, infrastructure failure, and broken internal state as the same thing.
They are not the same thing.
A contract-aware design keeps those categories separate. The tool can vary: exceptions, result types, railway-oriented programming, typed errors, validation libraries, algebraic data types, or plain objects. The distinction should not vary.
Exceptions, failures, and broken contracts
Exceptions are where contract thinking often becomes blurry.
A broken contract and an expected outcome are not the same thing.
Meyer’s framing helps: a routine fails when it cannot fulfill its contract. An exception is the abnormal event that disrupts normal execution. Exception handling is not a license to turn ordinary business alternatives into jumps through the call stack.
If a caller passes an invalid value to an internal operation whose precondition clearly rejects that value, an exception or assertion failure may be appropriate. The program was used incorrectly.
If a supplier finishes an operation and its postcondition is false, an exception or assertion failure may be appropriate. The supplier failed to keep its promise.
If an object exposes a state that violates its invariant, an exception or assertion failure may be appropriate. The abstraction is no longer trustworthy.
If a customer tries to reserve seats and there are not enough seats, that may be a normal business outcome. The caller did not necessarily misuse the program. The business said no.
The useful categories are:
Precondition violation: invalid call.
Postcondition violation: supplier defect.
Invariant violation: inconsistent abstraction.
Expected rejection: valid business refusal.
Operational failure: dependency, network, persistence, or platform problem.
Recovery also has a contract. If an operation handles an abnormal situation, it must either restore a meaningful state and retry, or report failure in a way that the caller can understand. Half-recovery is often worse than no recovery because it leaves the abstraction pretending to be alive while its invariant is already broken.
Subtyping is subcontracting
Contracts become sharper when polymorphism enters the room.
If an implementation is used through an interface, it is a subcontractor. It must not make life harder for clients of the interface.
In contract terms:
A subtype must not require more than the supertype promised.
A subtype must not deliver less than the supertype promised.
A subtype must preserve inherited invariants.
A subtype must not surprise clients that only know the abstraction.
This aligns with Liskov and Wing’s behavioral subtyping: properties expected of supertype objects should still hold for subtype objects.
A bad language-neutral example:
interface DiscountPolicy
discountFor(customer)
implementation LoyaltyDiscountPolicy
discountFor(customer)
require customer is loyal
return 10 percentThe interface says, “I can calculate a discount for a customer.” The implementation says, “Actually, only loyal customers.”
That strengthens the precondition. A caller using the interface cannot know this. The implementation has betrayed the interface.
There are two better designs. One option makes the operation total over all customers:
discountFor(customer)
if customer is loyal
return 10 percent
otherwise
return 0 percentAnother option narrows the input type so the contract becomes visible:
discountFor(loyalCustomer)
return 10 percentThese designs say different things. The first says, “This policy can answer for every customer.” The second says, “This policy only applies after loyalty has already been established.”
The mistake is the middle ground: a broad interface with a narrow implementation.
This applies beyond classical inheritance. It applies to interface implementations, abstract classes, TypeScript structural types, protocols, traits, typeclasses, adapters, plugin systems, and public APIs.
The review question is simple: an this implementation be used wherever the abstraction is expected without surprising the caller?
If not, the contract is lying.
The Ariane lesson
Ariane 5 Flight 501 is often used as a software cautionary tale because it is dramatic: on June 4, 1996, the launcher failed roughly 40 seconds after the flight sequence began. The inquiry board traced the technical chain to the inertial reference system software. A conversion from a 64-bit floating-point value to a 16-bit signed integer raised an operand error because the value was too large. Both redundant inertial reference systems used the same software and failed for the same reason.
The simple story is “an overflow crashed a rocket.”
The contract story is more useful.
The relevant software came from Ariane 4. The assumption behind the unprotected conversion was valid for Ariane 4’s trajectory but not for Ariane 5’s. The inquiry board noted that the Ariane 5 trajectory data had not been included in the relevant inertial reference system requirements and specification. Jézéquel and Meyer’s “Design by Contract: The lessons of Ariane” makes the software reuse lesson explicit: the reusable module carried an operating condition, but that condition was not attached to the reusable element where reuse needed it.
That does not mean contracts magically prevent disasters. It means reusable software without visible operating conditions is dangerous.
The deeper lesson is not “check every conversion forever.” Engineering always has cost, performance, and complexity trade-offs. The deeper lesson is that a decision not to check something is only defensible when the reason travels with the software element that depends on it.
Invisible assumptions are not free. They accumulate interest.
Translating contracts into modern languages
The method is language-agnostic. The implementation is not.
Eiffel has native contract syntax. Many modern languages do not. Some have strong type systems, sum types, and exhaustive pattern matching. Some rely more on runtime checks. Some provide annotations, macros, libraries, or conventions. Some make illegal states difficult to represent. Some make runtime validation unavoidable.
The contract thinking remains transferable.
Translations differ by language:
In Kotlin, data classes,
require,check, sealed interfaces, exhaustivewhen, and private constructors are useful.In C#, records, value objects, nullable reference types, guard clauses, contracts by convention, and result types are common.
In Java, records, sealed classes, constructor or factory validation, and explicit result objects can express many ideas.
In TypeScript, compile-time types must be paired with runtime parsing at boundaries because type information does not protect runtime input.
In PHP, readonly value objects, named constructors, exceptions, and explicit result objects are practical tools.
In Rust, newtypes,
Result, enums, ownership, and pattern matching support strong contract expression.In Python, dataclasses, type hints, validators, runtime assertions, and clear module boundaries can help, but runtime checks remain important.
In Go, small interfaces, explicit errors, constructor functions, and package-level encapsulation often carry the agreement.
The point is not to force every language into Eiffel’s shape. The point is to preserve the question: who owes what to whom, and how does the code make that visible?
Case study: a Kotlin reservation model
Now let’s make the idea concrete.
The business rule is small enough to fit in one paragraph: a customer can request seats for a flight. The request must contain a valid number of seats. If enough seats are available, the reservation is accepted and availability decreases by exactly that number. If there are not enough seats, the reservation is rejected and availability remains unchanged.
That gives us:
A valid seat request before entering the domain model.
Non-negative availability as an invariant.
A postcondition for accepted reservations.
An expected domain rejection for insufficient capacity.
A result type that documents all business outcomes.
Domain concepts
import kotlin.uuid.Uuid
data class FlightId private constructor(
val value: Uuid,
) {
companion object {
fun from(value: Uuid): FlightId =
FlightId(value)
fun fromExternal(value: String): FlightId? =
Uuid.parseOrNull(value)?.let(::from)
}
}
data class ReservationId private constructor(
val value: Uuid,
) {
companion object {
fun from(value: Uuid): ReservationId =
ReservationId(value)
}
}
data class RequestedSeats private constructor(
val value: Int,
) {
companion object {
const val MinimumSeats = 1
const val MaximumSeats = 9
fun from(value: Int): RequestedSeats {
require(value in MinimumSeats..MaximumSeats) {
"Requested seats must be between $MinimumSeats and $MaximumSeats"
}
return RequestedSeats(value)
}
fun fromExternal(value: Int): RequestedSeats? =
if (value in MinimumSeats..MaximumSeats) {
from(value)
} else {
null
}
}
}
data class AvailableSeats private constructor(
val value: Int,
) {
fun canCover(requestedSeats: RequestedSeats): Boolean =
value >= requestedSeats.value
fun afterReserving(requestedSeats: RequestedSeats): AvailableSeats {
require(canCover(requestedSeats)) {
"Requested seats must be available"
}
return from(value - requestedSeats.value)
}
companion object {
fun from(value: Int): AvailableSeats {
require(value >= 0) {
"Available seats must not be negative"
}
return AvailableSeats(value)
}
}
}RequestedSeats makes invalid request sizes impossible inside the domain. AvailableSeats protects the non-negative invariant. FlightId.fromExternal belongs at the boundary because external text may fail to become a valid identifier.
Kotlin documents require as a way to validate input arguments and check as a way to validate object or variable state. That distinction maps well to contract thinking: require fits caller obligations, while check fits supplier state, postconditions, and invariant checks.
Domain decision
data class Reservation(
val id: ReservationId,
val flightId: FlightId,
val seats: RequestedSeats,
)
sealed interface ReservationDecision {
data class Accepted(
val reservation: Reservation,
val updatedAvailability: FlightAvailability,
) : ReservationDecision
sealed interface Rejected : ReservationDecision {
data class NotEnoughSeats(
val currentAvailability: FlightAvailability,
) : Rejected
}
}
class FlightAvailability private constructor(
val flightId: FlightId,
val availableSeats: AvailableSeats,
) {
fun reserve(
requestedSeats: RequestedSeats,
reservationId: ReservationId,
): ReservationDecision {
if (!availableSeats.canCover(requestedSeats)) {
return ReservationDecision.Rejected.NotEnoughSeats(
currentAvailability = this,
)
}
val updatedAvailability = FlightAvailability(
flightId = flightId,
availableSeats = availableSeats.afterReserving(requestedSeats),
)
return ReservationDecision.Accepted(
reservation = Reservation(
id = reservationId,
flightId = flightId,
seats = requestedSeats,
),
updatedAvailability = updatedAvailability,
)
}
companion object {
fun open(
flightId: FlightId,
availableSeats: AvailableSeats,
): FlightAvailability =
FlightAvailability(
flightId = flightId,
availableSeats = availableSeats,
)
}
}This design is immutable. Meyer’s Eiffel examples often use commands that mutate objects and express postconditions through old values and queries. This Kotlin version translates the same contract idea into an immutable state transition: an accepted reservation returns a new FlightAvailability.
The contract is visible:
RequestedSeatsrejects invalid request sizes.AvailableSeatsrejects impossible availability.reservereturns eitherAcceptedorNotEnoughSeats.Acceptedincludes the reservation and updated availability.NotEnoughSeatsincludes current availability, making the rejection state observable.The non-negative availability invariant is owned by
AvailableSeats, soFlightAvailabilitycan compose an already-valid concept.
Notice what is not happening: NotEnoughSeats is not an exception. It is a domain decision.
That distinction is the contract.
Application boundary
The outside world gives us strings and numbers. The domain should receive concepts.
data class ReserveSeatsCommand(
val flightId: String,
val seats: Int,
)
sealed interface ReserveSeatsResult {
data class Accepted(
val reservationId: String,
) : ReserveSeatsResult
sealed interface Rejected : ReserveSeatsResult {
data object InvalidFlightId : Rejected
data object InvalidSeatCount : Rejected
data object FlightNotFound : Rejected
data object NotEnoughSeats : Rejected
}
}
interface FlightAvailabilityRepository {
fun findBy(flightId: FlightId): FlightAvailability?
fun save(availability: FlightAvailability)
}
interface ReservationIds {
fun next(): ReservationId
}
class ReserveSeatsHandler(
private val repository: FlightAvailabilityRepository,
private val reservationIds: ReservationIds,
) {
fun handle(command: ReserveSeatsCommand): ReserveSeatsResult {
val flightId = FlightId.fromExternal(command.flightId)
?: return ReserveSeatsResult.Rejected.InvalidFlightId
val requestedSeats = RequestedSeats.fromExternal(command.seats)
?: return ReserveSeatsResult.Rejected.InvalidSeatCount
val availability = repository.findBy(flightId)
?: return ReserveSeatsResult.Rejected.FlightNotFound
return when (
val decision = availability.reserve(
requestedSeats = requestedSeats,
reservationId = reservationIds.next(),
)
) {
is ReservationDecision.Accepted -> {
repository.save(decision.updatedAvailability)
ReserveSeatsResult.Accepted(
reservationId = decision.reservation.id.value.toString(),
)
}
is ReservationDecision.Rejected.NotEnoughSeats ->
ReserveSeatsResult.Rejected.NotEnoughSeats
}
}
}The handler separates categories:
InvalidFlightIdis boundary input rejection.InvalidSeatCountis boundary input rejection.FlightNotFoundis an application result.NotEnoughSeatsis a domain decision.An invariant violation would be a programming defect.
The example is about the contract, not Kotlin. In another language, the shapes would change. The responsibility split should survive.
Tests as contract witnesses
Tests are not the contract itself. Tests are witnesses.
A contract states the agreement. A good test demonstrates that visible behavior respects that agreement in a meaningful scenario. It should not inspect private fields. It should not mock internal structure. It should not care whether the implementation uses a list, an array, an event, or a private helper.
It should ask what the client can observe. The test code below keeps that witness role visible.
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNull
import kotlin.uuid.Uuid
class RequestedSeatsShould {
@Test
fun givenMinimumExternalValue_whenParsingRequestedSeats_thenReturnsRequestedSeats() {
assertEquals(
expected = RequestedSeats.from(1),
actual = RequestedSeats.fromExternal(1),
)
}
@Test
fun givenAboveMaximumExternalValue_whenParsingRequestedSeats_thenReturnsNull() {
assertNull(RequestedSeats.fromExternal(10))
}
@Test
fun givenZeroRequestedSeats_whenCreatingRequestedSeats_thenRejectsValue() {
assertFailsWith<IllegalArgumentException> {
RequestedSeats.from(0)
}
}
}
class FlightAvailabilityShould {
private val flightId =
FlightId.from(Uuid.parse("4e5071c2-f3b4-4c47-9e0f-4f6363c4c8d1"))
private val reservationId =
ReservationId.from(Uuid.parse("42acfe18-e0e5-48c0-83f5-13a7c0c28d4c"))
@Test
fun givenEnoughSeats_whenReservingSeats_thenReducesAvailabilityByRequestedSeats() {
val availability = FlightAvailability.open(
flightId = flightId,
availableSeats = AvailableSeats.from(3),
)
val decision = availability.reserve(
requestedSeats = RequestedSeats.from(2),
reservationId = reservationId,
)
val accepted = acceptedDecisionOf(decision)
assertEquals(
expected = AvailableSeats.from(1),
actual = accepted.updatedAvailability.availableSeats,
)
}
@Test
fun givenEnoughSeats_whenReservingSeats_thenReturnsReservationForRequestedSeats() {
val availability = FlightAvailability.open(
flightId = flightId,
availableSeats = AvailableSeats.from(3),
)
val decision = availability.reserve(
requestedSeats = RequestedSeats.from(2),
reservationId = reservationId,
)
val accepted = acceptedDecisionOf(decision)
assertEquals(
expected = RequestedSeats.from(2),
actual = accepted.reservation.seats,
)
}
@Test
fun givenNotEnoughSeats_whenReservingSeats_thenRejectsReservation() {
val availability = FlightAvailability.open(
flightId = flightId,
availableSeats = AvailableSeats.from(1),
)
val decision = availability.reserve(
requestedSeats = RequestedSeats.from(2),
reservationId = reservationId,
)
val rejected = notEnoughSeatsDecisionOf(decision)
assertEquals(
expected = availability,
actual = rejected.currentAvailability,
)
}
private fun acceptedDecisionOf(
decision: ReservationDecision,
): ReservationDecision.Accepted =
when (decision) {
is ReservationDecision.Accepted -> decision
is ReservationDecision.Rejected.NotEnoughSeats ->
error("Expected accepted reservation")
}
private fun notEnoughSeatsDecisionOf(
decision: ReservationDecision,
): ReservationDecision.Rejected.NotEnoughSeats =
when (decision) {
is ReservationDecision.Accepted ->
error("Expected not enough seats rejection")
is ReservationDecision.Rejected.NotEnoughSeats -> decision
}
}Each test has one reason to fail. The parser tests witness the valid lower boundary, the invalid upper boundary, and invalid construction separately. The reservation tests witness the postcondition about availability, the returned reservation, and the rejected domain outcome without inspecting private representation.
There is no test for private representation because private representation is not part of the client contract. If a test knows too much about the inside, it becomes another hidden clause. It tells the implementation, “I know how you work inside, and I will punish you if you change.”
That is not a witness. That is a hostage note.
Kotlin contracts are not Meyer contracts
Kotlin has a package named kotlin.contracts.
The name can mislead.
Kotlin contracts let functions describe certain effects to the compiler, such as returning normally, implying a condition, or calling a lambda in place. The API for declaring custom function contracts remains experimental and requires opt-in.
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
@OptIn(ExperimentalContracts::class)
fun <T : Any> T?.mustExist(name: String): T {
contract {
returns() implies (this@mustExist != null)
}
return requireNotNull(this) {
"$name must not be null"
}
}This can help the compiler reason about nullability after the function returns.
It is useful. It is not Meyer’s Design by Contract.
It does not express a domain postcondition such as “an accepted reservation decreases availability by requested seats.” It does not model business rejection. It does not preserve aggregate invariants. It does not decide who owns a condition.
Use Kotlin contracts when they help the compiler understand a small function. Use Design by Contract to design collaboration.
They share a word. They do not share the same scope.
When contracts hurt
Contracts are powerful because they reduce ambiguity. They hurt when they increase it.
They hurt when the same check appears in every layer without responsibility being clarified. They hurt when expected business outcomes become exceptions. They hurt when a class exposes technical errors instead of domain decisions. They hurt when assertions repeat implementation details instead of stating observable promises. They hurt when a subtype quietly changes what its interface appeared to allow.
They also hurt when they are used as blame. A contract is not a way to say, “You called me wrong, so I do not care.” It is a way to make correct collaboration easier than incorrect collaboration.
Runtime monitoring needs judgment. Some contracts should be checked in development. Some should be checked during tests. Some critical invariants may deserve production monitoring. Some contracts are better expressed through types, construction rules, parsers, or result models. Some postconditions are better witnessed by tests and documentation than checked after every assignment.
There is no useful rule that says “check everything at runtime.” There is no useful rule that says “trust everything because the contract says so.”
The right question is: where does this contract give the most safety for the least confusion?
Practical checklist
Use this checklist when reviewing code through a contract lens.
For an operation
Ask:
What must be true before this operation is called?
Is that condition a caller obligation, a boundary parsing concern, or an expected business case?
What does the operation promise after success?
Is the promise visible through returned values, public queries, state transition, or events?
What remains true no matter which successful path is taken?
Are postconditions expressed through observable behavior rather than private representation?
For a domain type, object, module, or aggregate
Ask:
Can this abstraction represent an invalid value?
Is a primitive hiding a domain concept?
Should construction reject invalid values?
Should external parsing return a typed rejection, nullable result, or validation result instead of throwing?
Does the abstraction expose mutable internals?
Can another object break its invariant?
Is the invariant established at creation?
Is the invariant preserved after exported operations?
For an application boundary
Ask:
Where does untrusted input become domain data?
Are malformed input, missing data, business rejection, and infrastructure failure modeled separately?
Does the domain receive concepts or raw transport data?
Are external identifiers parsed at the edge?
Does the boundary avoid swallowing broad failures?
For an interface, subtype, adapter, or plugin
Ask:
Can every implementation accept what the abstraction appears to accept?
Does any implementation strengthen the precondition?
Does any implementation weaken the postcondition?
Does any implementation violate inherited invariants?
Should the input type be narrower?
Should the result type be richer?
Do contract tests verify the behavior expected from all implementations?
For a test
Ask:
Does the test verify behavior?
Does it fail for one clear reason?
Does it assert only what matters for that behavior?
Does it avoid private state?
Does it witness a precondition, postcondition, invariant, or explicit outcome?
The goal is not more ceremony. The goal is fewer hidden clauses.
Conclusion
A lot of software fails because the agreement was never written where the code could see it.
The caller brought the wrong value. The supplier returned less than it promised. The object exposed an impossible state. The subtype changed the deal. The boundary passed weather into the domain and hoped the domain would stay dry.
Design by Contract gives us a sharper language for these failures.
A precondition says: “Bring this.”
A postcondition says: “I will deliver that.”
An invariant says: “I will remain whole.”
Meyer’s deeper lesson is that contracts are not an isolated technique. They connect the class, the abstract data type, the reliability discipline, and inheritance. They bring semantics back into the software text. They make documentation more precise. They make tests more meaningful. They make exception handling less vague. They make reuse safer because operating conditions travel with the module.
The idea is not tied to Eiffel, Kotlin, Java, C#, TypeScript, PHP, Python, Rust, or any other single language. Each language gives different tools. The discipline is the same: make obligations explicit, make promises observable, protect invariants, and keep expected outcomes separate from broken assumptions.
The point is not to write more checks. The point is to remove guesswork.
Do not ask objects to read your mind. Do not ask tests to police private machinery. Do not ask callers to discover hidden clauses in production. Do not ask reusable code to carry assumptions in silence.
Say the agreement out loud. Then make the code keep it.
References
Apt, Krzysztof R., Olderog, Ernst-Rüdiger. Fifty years of Hoare’s logic. https://link.springer.com/article/10.1007/s00165-019-00501-3
Estler, H.-Christian, Furia, Carlo A., Nordio, Martin, Piccioni, Marco, Meyer, Bertrand. Contracts in practice. https://arxiv.org/abs/1211.4775
Ferreira, David R., Mendes, Alexandra, Ferreira, João F. Contract usage and evolution in Android mobile applications. https://arxiv.org/abs/2401.14244
JetBrains. Exception and error handling. https://kotlinlang.org/docs/exceptions.html
JetBrains. ExperimentalContracts. https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.contracts/-experimental-contracts/
JetBrains. Inline value classes. https://kotlinlang.org/docs/inline-classes.html
JetBrains. kotlin.contracts. https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.contracts/
JetBrains. Sealed classes and interfaces. https://kotlinlang.org/docs/sealed-classes.html
JetBrains. UUIDs. https://kotlinlang.org/docs/uuids.html
JetBrains. What’s new in Kotlin 2.3.0. https://kotlinlang.org/docs/whatsnew23.html
JetBrains. What’s new in Kotlin 2.4.0. https://kotlinlang.org/docs/whatsnew24.html
Jézéquel, Jean-Marc, Meyer, Bertrand. Design by Contract: The lessons of Ariane. https://se.inf.ethz.ch/~meyer/publications/computer/ariane.pdf
Lions, J. L. ARIANE 5 Flight 501 failure: Report by the inquiry board. https://www-users.cse.umn.edu/~arnold/disasters/ariane5rep.html
Liskov, Barbara H., Wing, Jeannette M. A behavioral notion of subtyping. https://www.cs.cmu.edu/~wing/publications/LiskovWing94.pdf
Meyer, Bertrand. Applying “Design by Contract”. https://se.inf.ethz.ch/~meyer/publications/computer/contract.pdf
Meyer, Bertrand. Class invariants: Concepts, problems, solutions. https://arxiv.org/abs/1608.07637
Meyer, Bertrand. Design by Contract. https://se.inf.ethz.ch/~meyer/publications/old/dbc_chapter.pdf
Meyer, Bertrand. Object-Oriented Software Construction, second edition. https://bertrandmeyer.com/wp-content/upLoads/OOSC2.pdf