A behavior-preserving refactor that makes unintended states structurally impossible using Sum Types (Enums, Discriminated Unions, Sealed Classes).
- Make Illegal States Unrepresentable: Structure types so invalid combinations cannot compile—no runtime validation or comments needed.
- Sum Types over Product Types: Replace structs with optional fields (
status,data?,error?) with enums where each variant holds exactly what it needs. - Pattern Matching: Replace boolean checks (
if isX) with exhaustivematch/switch. Adding a new state forces compiler errors at all call sites.