Case file F6

Encapsulation

The interface is the only contract. The implementation is a design secret.

Parnas wrote it down in 1972 and the wording has not aged: every module is characterised by its knowledge of a design decision which it hides from all others. Its interface is chosen to reveal as little as possible about its inner workings. That is the rule and it is the foundation that makes change cheap. A unit whose implementation is private can be rewritten without touching its callers. A unit whose implementation has leaked has fused itself to every callsite that knows about its insides.

ByAdam LewisPublished1 May 2026Revised13 Oct 2026Reading14 minVersionv1.2ConfidenceHigh
§0b

Opinion

I've watched modern codebases pretend to follow F6 because the language provides the keywords. They have private fields, protected methods and exports with names on them. The syntactic surface looks encapsulated. The practice diverges. The class exposes a getter and setter for every private field, so the encapsulation is decoration. The module exports a helper a single caller uses, so the boundary is theatrical. The function takes a flag that lets the caller bypass an invariant, so the body is no longer a secret. The keyword is there. The discipline is not.

The pressure always runs the other way: more flexibility for the caller, more configurability, more introspection. Every one of those moves is easy to add and hard to remove: once a getter exists, callers find it and the implementation behind it is fixed forever. The fix is not more keywords. It is fewer affordances: the public surface is the smallest set of operations that lets callers do their job, and any state callers do not need to see is not reachable.

Agents read the public interface the same way reviewers do, which makes every leak more expensive, not less. If the implementation has leaked, the agent reads the leak and treats it as part of the contract. Within a session that may be benign. Across sessions and across teams the leak compounds into a structural dependency no-one notices until the body has to change. Encapsulation is the only practical defence against that compound: the linter floor is necessary, the review culture is what holds it up.

Copy a note and link

Grab this short comment and drop it into a PR comment or an LLM chat to prompt the right change.

Hide the inside of your code. Callers should only call methods: never read or change fields directly. If state has to change, give them a method that does it safely. Always mark every field and method as `public` or `private`; never rely on the language default.

/tenet/encapsulation/F6
Copy for paste
§0c

AI eyes only

Rule: the interface is the only contract. Public mutable state is a defect.

Reject: pairs of getters and setters that expose private fields. Reject: public mutable members. Reject: default-visibility on classes that hold state. Reject: returning internal collections by reference.

Generate: behavioural methods that operate on internal state and enforce invariants. Constructors that validate. private (or #-prefix) by default. Only promote to public when a caller demonstrates the need.

Diagnostic: if removing a public member would mean a single-line rename in callers, the member is leaking implementation. Replace with a behavioural method named for the operation.

§0d

Why?

  • The body is free to change while the contract holds. The cost of a refactor is bounded to the unit, not its callers.
  • The unit is testable through its interface alone. Encapsulation produces the boundary TS7 Test Isolation wants to mock at.
  • Reviewers read the public surface to validate behaviour. They don't need to read the body. Smaller surfaces mean faster reviews.
  • Behavioural subtyping (A9 LSP) only works when the contract is stable. F6 makes LSP enforceable. Without encapsulation, every subtype can be broken by a parent-class internals leak.
  • Private mutable state is debuggable. Public mutable state is a race. Encapsulation is the cheapest form of concurrency safety we have.
  • Pairs with T1 Domain-Driven Types: the type encodes the contract; encapsulation enforces that callers cannot construct invalid states by reaching into the body.
  • Agent-generated code stays composable when the public surface is small. Implementation leaks turn into agent-encoded contracts that compound across sessions.
The receipts
Origins, quoted passages, evidence, the strongest counter-argument and the reply.
§1

Origins

David Parnas wrote the foundational paper in 1972. On the Criteria to be Used in Decomposing Systems into Modules presented two decompositions of a single program (the KWIC index) and showed that the information hiding decomposition produced cheaper change, while the flowchart-based decomposition produced fragile code that propagated changes across modules. The argument has not been bettered: every module is characterised by its knowledge of a design decision which it hides from all others.1

Barbara Liskov extended the argument to types in 1987. The Liskov Substitution Principle is, at heart, an encapsulation rule: a subtype must honour the encapsulated promises of its supertype, or substitution silently breaks. LSP stops encapsulation being decorative by pushing the discipline into the type system rather than leaving it at the keyword level.2

Bertrand Meyer 1988 operationalised encapsulation as Design by Contract: pre- conditions, post-conditions, and class invariants are the executable form of the encapsulated interface. Eiffel made these first-class language constructs; TypeScript's readonly, Rust's ownership model, and modern type-state APIs are all descendants. Contracts make the body a secret you can trust.3

The Gamma, Helm, Johnson, Vlissides 1994 catalogue codified the practical lever: encapsulate what varies. Every behavioural pattern (Strategy, State, Command, Observer) is an encapsulation move, and every structural pattern (Adapter, Bridge, Facade, Proxy) is one too. The patterns are not arbitrary. They are twenty-three solutions to the same recurring problem of which design decisions to hide and how to hide them.4

Joshua Bloch 2018 ported the principle into modern Java idiom as Item 15: Minimize the accessibility of classes and members. The justification is Parnas's: information hiding decouples the components that comprise a system, allowing them to be developed, tested, optimised, used, understood, and modified in isolation. Forty-six years separate the two formulations. The rule is the same.5

§2

Quotes

Every module in the second decomposition is characterized by its knowledge of a design decision which it hides from all others. Its interface or definition was chosen to reveal as little as possible about its inner workings.

David Parnas · On the Criteria to be Used in Decomposing Systems into Modules (1972)

Under the Design by Contract theory, a software system is viewed as a set of communicating components whose interaction is based on precisely defined specifications of the mutual obligations — contracts.

Bertrand Meyer · Object-Oriented Software Construction (1988/1997)

Program to an interface, not an implementation. … Favor object composition over class inheritance.

Gang of Four · Design Patterns, Ch. 1 (1994), via Wikipedia

Information hiding is important for many reasons, most of which stem from the fact that it decouples the components that comprise a system, allowing them to be developed, tested, optimized, used, understood, and modified in isolation.

Joshua Bloch · Effective Java, 3rd ed. · Item 15
§3

Evidence

15 external sources, ranked by author authority. The first five are the canon; expand to see the rest, including the qualifiers and the named opposers. Each links out to its primary source.

  1. 01
    David Parnas · 1972
    The foundational paper. Information hiding as the criterion for decomposition. The KWIC example shows the cost of getting it wrong.
  2. 02
    Barbara Liskov · 1987
    Behavioural subtyping. Encapsulation enforced through types: a subtype must honour the supertype's encapsulated contract or substitution breaks.
  3. 03
    Bertrand Meyer · 1988/1997
    Design by Contract, preconditions, postconditions, invariants, is the executable form of the encapsulated interface.
  4. 04
    Gamma, Helm, Johnson, Vlissides · 1994
    'Encapsulate what varies' is the catalogue's through-line. Every pattern is an encapsulation move.
  5. 05
    Joshua Bloch · 2018
    The modern Java textbook formulation. The rationale is Parnas's, undimmed by 46 years.
  1. 06
    Robert C. Martin · 2008
    Distinguishes objects (which encapsulate behaviour and hide state) from data structures (which expose state and have no behaviour). The chapter is essentially F6 as a Java textbook.
  2. 07
    Hunt & Thomas · 1999/2019
    Tell-Don't-Ask is the operational form of F6: methods over field access. Behaviour over state inspection.
  3. 08
    Martin Fowler · 2013
    Short essay reformulating the rule for the OO community. The pattern is encapsulation in two words.
  4. 09
    principles-wiki contributors · live
    The canonical taxonomy treats Information Hiding and Encapsulation as a single principle with two names.
  5. 10
    John Ousterhout · 2018
    Modern academic restatement. Important qualifier: Ousterhout argues that *deep modules*, small interfaces over substantial behaviour, are the operational target. Shallow modules with trivial encapsulation defeat the purpose.
  6. 11
    Gerard Holzmann · 2006
    Safety-critical analogue: data must be declared at the smallest possible scope. F6 enforced by static analysis with zero-warning policy.
  7. 12
    Microsoft TypeScript team · live
    Modern operationalisation: TypeScript supports both keyword visibility (public/private/protected) and JavaScript native #private fields. The latter is genuinely runtime-private.
  8. 13
    Liskov & Wing · 1994
    Formalisation of the 1987 keynote. Defines subtyping in terms of *behavioural compatibility*, the encapsulated contract, rather than syntactic structure.
  9. 14
    Gamma, Helm, Johnson, Vlissides · 1994
    Inheritance leaks the parent's implementation into the subclass. Composition keeps each unit's body private. CRP is encapsulation applied to reuse.
  10. 15
    David Parnas · 1972
    Parnas himself separates two properties: his information-hiding decomposition does yield a Dijkstra-style hierarchy, but he concludes that hierarchical structure and 'clean' decomposition are 'two desirable but independent properties of a system structure'. Hiding is not the same thing as layering.

Fifteen sources across five decades. The supporting line runs unbroken from Parnas in 1972 through Liskov, Meyer, GoF, and Bloch. The qualifying voice extends rather than challenges: shallow modules with trivial encapsulation are no improvement over leaks. The operational target is deep modules with substantial behaviour behind narrow interfaces. The corpus has no opposing line. Encapsulation is the most stable principle in the Foundations.

§4

Examples

Viewing: TypeScript.
Avoid

Before callers maintain the invariant by hand. One forgets and total drifts.

Fileshopping-cart.ts
class ShoppingCart {
  items: Item[] = [];
  total: number = 0;
}
checkout.tscaller code, scattered across files
cart.items.push(item);
cart.total += item.price;
gift-basket.tselsewhere, the second caller forgets
otherCart.items.push(item); // total now lies
Prefer

After state is private; the invariant lives inside the unit.

Fileshopping-cart.ts
class ShoppingCart {
  private readonly items: Item[] = [];
  private total = 0;

  public add(item: Item): void {
    this.items.push(item);
    this.total += item.price;
  }

  public getTotal(): number { return this.total; }
}
checkout.tsthe invariant cannot drift
cart.add(item);
§4b

Enforcement

Viewing: TypeScript.

Apply these rules in eslint.config.mjs. The full enforcement across every tenet lives on the implementation page.

RuleToolCatches
@typescript-eslint/explicit-member-accessibilitytypescript-eslintclass members without an explicit access modifier: public-by-default is a leak by accident.
@typescript-eslint/no-explicit-anytypescript-eslintany-typed return values and parameters that erase the contract the interface promised.
@typescript-eslint/prefer-readonlytypescript-eslintprivate fields never reassigned: should be readonly, signalling immutability after construction.
@typescript-eslint/no-non-null-assertiontypescript-eslintthe ! operator: a lie to the compiler about your invariant. Prove it with a guard.
no-unused-private-class-membersESLint coreprivate members never read: dead encapsulated state.
eslint.config.mjsconfiguration snippet
import { defineConfig } from 'eslint/config';
import tseslint from 'typescript-eslint';

export default defineConfig({
  files: ['**/*.{ts,tsx}'],
  extends: [tseslint.configs.base],
  languageOptions: {
    parserOptions: { projectService: true, tsconfigRootDir: import.meta.dirname },
  },
  rules: {
    // Encapsulation hygiene: no leaking implementation as 'any'.
    '@typescript-eslint/no-explicit-any': 'error',
    '@typescript-eslint/explicit-module-boundary-types': 'error',
    '@typescript-eslint/no-non-null-assertion': 'error',

    // Class-level encapsulation: fields must declare visibility.
    '@typescript-eslint/explicit-member-accessibility': [
      'error',
      { accessibility: 'explicit' },
    ],
    '@typescript-eslint/parameter-properties': ['error', { prefer: 'parameter-property' }],
    'no-unused-private-class-members': 'error',

    // No mutable public state.
    '@typescript-eslint/prefer-readonly': 'error',
    '@typescript-eslint/prefer-readonly-parameter-types': 'off', // expensive; opt in per repo
  }
});
§4c

AI rules

File.cursor/rules/f6-encapsulation.mdc
---
alwaysApply: true
---

## Prickles F6: Encapsulation

Hide the inside of your code. Callers should only call methods: never read or change fields directly. The method names are the contract. The body of the code is private and free to change.

If state has to change, give callers a method that does the change safely. The method enforces the rules. The field stays private. Don't expose a field for the caller to update by hand.

Always mark every field and method as `public` or `private` on purpose. Never rely on the language default. If it's only used inside the unit, mark it `private`.

F6 is not the same as F1. F1 says each unit does one thing. F6 says each unit keeps its insides private. A focused class with public mutable fields passes F1 and fails F6: they catch different mistakes.

Why it matters: hidden internals make change cheap. You can rewrite the body without breaking any caller, because no caller depends on the body. Leaked internals fuse the unit to every callsite that touched them, and every change becomes a graph search.

Repo layout, CI, and ESLint wiring for these paths live on /implementation, not repeated on every tenet.

§5

Counter-argument

Counter

The steelman has two heads. The first is Ousterhout's: shallow modules with ceremonial encapsulation (getter for every field, public method for every internal operation) are no improvement on direct field access. They double the surface area while hiding nothing. The second is the value-object case: some objects (DTOs, plain records, schemas) are supposed to expose their fields. Forcing accessors on them produces ceremony and noise. F6 naively applied produces JavaBean clutter or anaemic OO designs where every class is a shell of getters.

§6

Counter-argument retort

Reply

The first head is right and is the operational refinement F6 needs. Encapsulation is not a count of accessors; it is the depth of behaviour behind an interface. A module with twenty getters and no behaviour has not encapsulated anything. It has built a protocol-buffer in OO clothes. The principle Ousterhout names, deep modules, is the goal F6 is in service of. Treat narrow interface, substantial body as the criterion, not private keyword on every field.

The second head is also true and is absorbed by F6, not refuted by it. Value objects, DTOs, schemas, and language-level record / data class constructs are precisely the cases where the public state is the contract. Use them. The modern languages have first-class support. F6 says public mutable state is a defect; immutable value-objects are the case the rule has the language-level escape hatch for.

Default stance for behaviour-bearing units: visibility declared, public state private, Tell-Don't-Ask, narrow interface, substantial body. Default stance for value-bearing units: readonly, immutable, all-public-by-design. Anything else is an exception that gets defended in the PR description.

§7

Notes

  1. [1]David Parnas: On the Criteria to be Used in Decomposing Systems into Modules: CMU TR CMU-CS-71-101 (August 1971); Communications of the ACM 15(12), 1053-1058 (December 1972). The foundational paper. The KWIC index example begins on p. 3 of the technical report. The information-hiding criterion is in the section titled 'The Criteria' (p. 17).
  2. [2]Barbara Liskov: Data Abstraction and Hierarchy: OOPSLA '87 keynote, addendum published in ACM SIGPLAN Notices 23(5), 17-34, May 1988. Formalised as the Liskov Substitution Principle by Liskov & Wing (1994).
  3. [3]Bertrand Meyer: Object-Oriented Software Construction (2nd ed., Prentice-Hall 1997). Design by Contract, preconditions, postconditions, class invariants, as the executable form of the encapsulated interface. Eiffel was the first language with first-class contract syntax.
Disagree? Found a hole in the argument? Take issue with this tenet →
Last revised: 2026-10-13