Software engineering / Open/closed principle

Open/closed principle

A checkout must support a new shipping method. We first do it by editing old code, then rebuild it so the next method is added without touching what already works.

Intermediate7 steps~6 min
Checkout — the working code we want to leave alone
Extension point — the agreed shape: cost(kg)
Shipping class — one method, one rule for the cost
New code — added later, without edits elsewhere
// THE SHORT VERSION

In 30 seconds

The open/closed principle says code should be open for extension but closed for modification: you add behaviour by writing new code, not by editing code that already works.

The usual way is an extension point. The stable code calls an agreed operation, and each variant is a separate class that provides it. Calling one operation and getting different behaviour is polymorphism.

It is a tool, not a law. For a couple of cases that never change, a plain if is simpler and better.

Open/closed principle, step by step

STEPS01 / 07

Simplified: one line somewhere must still choose which class to create. The principle keeps that choice in one place; it does not remove it. Prices are made up.

STEP 01 / 07 One function, every case
THE MATH
standard: 3 + kg express: 8 + 2 × kg order: 2 kg
  1. Step 1: One function, every case. A shop’s checkout works out the shipping cost with one function and an if for each shipping method. For a 2 kg order, standard costs 3 + 2 = €5 and express costs 8 + 2 × 2 = €12. The code is short and clear, and the checkout knows every method by name.
  2. Step 2: A new case means surgery. The shop adds locker pickup at a flat €2. The cost function must be opened and edited, and so must every other function that branches on the type, such as the label and the delivery estimate. Each edit touches code that was already tested and working, so all of it has to be checked again.
  3. Step 3: Open and closed. The open/closed principle, stated by Bertrand Meyer in 1988, says a piece of software should be open for extension but closed for modification. You should be able to give it new behaviour by adding new code, while the code that already works stays untouched.
  4. Step 4: Build an extension point. To make that possible, the checkout stops naming shipping methods. It is handed some object, method, and asks it for the cost. All it requires is a shape: anything with a cost(kg) operation will do. That agreed shape, an interface, is the extension point where new code can attach.
  5. Step 5: Polymorphism fills the slot. Each shipping method becomes its own small class with its own cost operation. The checkout makes the same call every time, and the object it was handed decides which code runs: €5 from Standard, €12 from Express. One call with many possible behaviours is called polymorphism.
  6. Step 6: Extend without editing. Locker pickup is now one new class with a cost operation that returns 2. It plugs into the free slot, and the checkout, Standard and Express are not opened at all. Compare with step 2: three edits to old code have become one new file and nothing to re-test but the new class.
  7. Step 7: When it is overkill. An extension point costs extra files and an extra hop for every reader to follow. For two cases that never change, a plain if is the better code. The interface also protects against only one kind of change: adding a new operation, such as eta(), would mean editing every class. Many teams wait until variation actually shows up.
VALUES SO FAR
standard3 + 2 = €5case
express8 + 2×2 = €12case
locker····problem
closed····rule
open····rule
slot····interface
locker····result
checkout····result
overkill····caveat

Spotted a mistake on this page?