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









