liveBook · Manning

Open-closed principle

As explained in Grokking Software Architecture

A software design principle stating that software entities should be open to extension but closed to modification.

The open/closed principle (OCP) says that software entities should be open to extension but closed to modification. Add new behavior by writing new code that plugs into a stable contract, rather than repeatedly changing code that already works. section 3.4.2 section 3.11

Think of a soccer midfielder: a coach can add plays to the playbook without rewriting the player's core job. In software, the shared contract is the playbook, and new classes provide the plays. This separates stable behavior from the parts likely to change and reduces dependence on concrete details. section 3.4.2

From branches to plays

A midfielder's executePlay method can choose a move with an if/else chain. But each new move means editing that method, making it larger and risking bugs in working code. Instead, give each play an execute method and pass the play to the midfielder. The midfielder checks for execute and calls it; a new play can be added as a new class without changing the midfielder. section 3.4.2

Listing 3.5 “Before”: Violating OCP - Modification
class Midfielder
{
     executePlay(playName)
     {
          if (playName === "DribblePastOpponent") 
          {
               console.log("Executing a dribble move…");
               // Dribbling logic here
          } else if (playName === "DefensiveFormation") {
               console.log("Getting into defensive position…");
               // Defensive logic here
          } else { ... } #A
     }
}

See how selecting plays with conditional branches ties new behavior to changes in the midfielder. section 3.4.2

Listing 3.6 “After”: Following OCP - Extension
 
// new class for a dribble move
class DribblePastOpponent 
{
     execute()
     {
          console.log(“Executing a dribble move…”);
          // Dribbling logic here
     }
}
 
//new class for a defensive formation
class DefensiveFormation
{
     execute()
     {
          console.log(“Getting into defensive position…”);
          // Defensive logic here
     }
}
 
class Midfielder #A
{
     executePlay(play)
     {
      if (typeof play.execute !== 'function') { #B
             throw new Error("Play must implement an execute() method!");
          }
          play.execute();  
     }
}

Compare the extension-based version, where each play supplies an execute method. section 3.4.2

The same idea works beyond individual classes. In publish-subscribe, an order service publishes an event without knowing its subscribers. Rewards and analytics services can subscribe without changing the order service. The tradeoff: asynchronous decoupling gives up the illusion of instant consistency. section 7.2.3

Look for a change that forces you to reopen stable code. For example, checkout logic tied directly to one payment provider must be rewritten when the provider changes or another is added. A stable abstraction can keep the core workflow separate from those concrete details. section 10.4.1 section 3.11

How Grokking Software Architecture covers it

How deep it goes
Explains the principle through contrasting implementations and extends it to service-level design, including a consistency tradeoff.
Examples
Uses soccer plays, a payment-provider-dependent checkout, and order events consumed by rewards and analytics services.
What you can do after
Evaluate code and service designs for changes that could instead be accommodated through stable contracts or subscriptions.
How it connects
Connects separation of concerns, loose coupling, abstractions, polymorphism, and publish-subscribe.

Also explained in

Compare how 4 books explain open-closed principle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.