jargon

Comparison

Algebraic data typevsReplace conditional with polymorphism

Algebraic data type

the type says it is exactly one of four things, and the compiler will not let you forget the fourth.

A type built by combining others as products — a record with several fields — or sums — one of several alternatives, each with its own data. The sum half is the one that changes how code is written: modelling a state, a result or a command as a closed set of alternatives makes the impossible combinations unrepresentable. It is the type-level version of the state pattern, and much harder to get subtly wrong.

Full entry →

Replace conditional with polymorphism

the same switch on type appeared in four methods, and adding a case meant finding all four.

The refactoring that moves each branch of a type-based conditional into a method on the corresponding subclass, so dispatch does the branching. It pays off when the same conditional is repeated across several operations; it does not pay off for a single switch in a single place, where a class hierarchy is more machinery than the branch it replaced. Recognising which case you have is the actual skill.

Full entry →

Related comparisons