---
title: "It Takes Two to Contract"
description: "Design by contract mostly reduces to types and assertions, but a real contract needs two parties: the same condition asserted at both the definition site and the call site."
url: "https://swe.quest/articles/it-takes-two-to-contract-371fa399d9d81617"
canonical: "https://tigerbeetle.com/blog/2023-12-27-it-takes-two-to-contract"
source: "TigerBeetle"
published: "2023-12-27"
difficulty: "medium"
kind: "Opinion"
reading_minutes: 6
tags: ["zig","testing","consensus","distributed-systems","language-design"]
---

# It Takes Two to Contract

A post on [TigerBeetle](https://swe.quest/sources/tigerbeetle.md), published 2023-12-27, 6 min read, Opinion. Original: [https://tigerbeetle.com/blog/2023-12-27-it-takes-two-to-contract](https://tigerbeetle.com/blog/2023-12-27-it-takes-two-to-contract)

## Summary

Design by contract mostly reduces to types and assertions, but a real contract needs two parties: the same condition asserted at both the definition site and the call site. In TigerBeetle, paired assertions (e.g., checking a function's return value both before returning and after the call) improve readability, act as an "airlock" for local reasoning, catch drift during refactoring, and even exposed a postcondition that was missing at the definition. The bigger payoff comes when the two halves are reached by different paths, such as sender and receiver replicas checking consensus invariants with different local knowledge, hash-chained checksums, or verifying that state rebuilt from a snapshot matches state built from replaying events. The recommendation is to look for the distant code that relies on each assertion and assert it there too.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Zig](https://swe.quest/tags/zig.md) (Languages)
- [Testing](https://swe.quest/tags/testing.md) (Engineering practices)
- [Consensus](https://swe.quest/tags/consensus.md) (Distributed systems)
- [Distributed systems](https://swe.quest/tags/distributed-systems.md) (Distributed systems)
- [Language design](https://swe.quest/tags/language-design.md) (Compilers & runtimes)

## Prerequisites

- assertions
- basic consensus protocols ([easy posts on Consensus](https://swe.quest/tags/consensus.md?difficulty=easy))

## Keywords

TigerBeetle, design by contract, Zig, NASA Power of 10, Nickel, hash-chaining, property-based testing

## Related posts

- [Snapshot Testing For the Masses](https://swe.quest/articles/snapshot-testing-for-the-masses-1d23ec44f416430c.md): TigerBeetle · 2024-05-14 · Medium · 8 min read · Testing, Zig, Developer tools
  A minimal snapshot testing library can be built without macros, external tools or editor integration: the core primitive is a self-updating string literal that records its source file and line (via Zig's @src()) and rewrites itself in place on mismatch.
- [Protocol-Aware Deterministic Simulation Testing](https://swe.quest/articles/protocol-aware-deterministic-simulation-testing-7f3b613be8faf2a6.md): TigerBeetle · 2026-08-20 · Hard · 10 min read · Formal methods, Testing, Consensus, Replication, Storage engines
  TigerBeetle's deterministic simulator (the VOPR) has full visibility into each replica's consensus and storage state, so it can check invariants that black-box tools like Jepsen or Antithesis cannot see through user-facing APIs.
- [The Tests Are Passing, Why Would I Read The Diff Again?](https://swe.quest/articles/the-tests-are-passing-why-would-i-read-the-diff-again-a7df681d6f6e3587.md): ClickHouse · 2021-04-14 · Medium · 10 min read · Code review, Documentation, Debugging
  Code review reduces defects largely because explaining code to another person exposes gaps in understanding, much as writing comments or rubber duck debugging does.
- [Random Fuzzy Thoughts](https://swe.quest/articles/random-fuzzy-thoughts-74b3f2f1b21a2499.md): TigerBeetle · 2023-03-28 · Medium · 9 min read · Testing, Formal methods, Distributed systems, Zig
  Treating a fuzzer's byte slice as a finite PRNG unifies coverage-guided fuzzing with structured property-based testing: the entropy length measures input complexity, gives test-case minimization almost for free, and tells interactive simulations when to stop injecting faults.
- [Swarm Testing Data Structures](https://swe.quest/articles/swarm-testing-data-structures-dcd11950dc8316b7.md): TigerBeetle · 2025-04-23 · Medium · 5 min read · Testing, Zig, Data structures
  Zig's comptime reflection (std.meta.DeclEnum) can turn a data structure's public API into an enum that drives a model-based property test, so adding a new public method triggers a compile error from the non-exhaustive switch until the test covers it.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
