liveBook · Manning

Type signature

As explained in Data-Oriented Programming in Java

A declaration of the types associated with a callable's inputs and output or another typed program element.

A type signature declares what a callable accepts and returns. By controlling those inputs and outputs, it narrows the space of valid implementations and makes domain assumptions visible instead of leaving them to guesswork. section 3.4.1 section 6.8

Making percentages explicit

A method such as computeFee(double total, double feePercent) permits ambiguous representations such as 1.5 or 150. Replacing the unrestricted value with a BoundedPercent type makes the expected range and fractional representation part of the signature, allowing invalid percentages to fail at construction rather than during later computation. section 3.4.1

Listing 3.29 A better world
record BoundedPercent(double numerator, double denominator) {  #A
    BoundedPercent {
        if (numerator > denominator && denominator == 0) {       #B
            throw new IllegalArgumentException(
                "Percentages are +/- 0..1 and must be expressed " +
                "as a proper fraction. e.g. 1/100 and have " +
                "a non-zero denominator");
        }
    }
}

Inspect the Java record that turns a percentage invariant into a checked domain type. section 3.4.1

A well-shaped signature helps by:

  • Making a function small enough that its data types suggest most of the implementation. section 6.3.1
  • Exposing missing or invented cases when a representation such as Map trades compile-time guarantees for runtime assumptions. section 6.3.1
  • Capturing invariants that would otherwise depend on informal assumptions, while allowing disagreement between types and existing code to guide refactoring. section 6.8

How Data-Oriented Programming in Java covers it

How deep it goes
Constrains implementations through inputs and outputs, while leaving business filtering logic such as past-due determination to the implementation.
Examples
Uses fee calculation, bounded percentages, past-due invoice collection, grace-period functions, and grace-period maps.
What it assumes
Assumes familiarity with Java methods and data types while teaching domain invariants through concrete types.
What you can do after
Build implementations from constrained inputs and outputs, and diagnose unsafe representations that defer guarantees until runtime.
How it connects
Connects functions, deterministic programming, domain invariants, the type system, lookup tables, and compile-time guarantees.

Also explained in

Compare how 10 books explain type signature →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.