Open/closed principle
Принцип открытости/закрытости
This design principle says to make new behavior possible without rewriting the code that already works. For example, you can add an output format to a report exporter without changing how it gathers data.
Also known as: OCP
What “open” and “closed” mean
The open/closed principle (OCP) is the O in SOLID: software components should be open for extension but closed for modification. A component is open when it provides a way to plug in new behavior. It is closed when adding that behavior does not require editing the component itself.
The important question is closed against what kind of change? An exporter might support new file formats without changes to its core. That doesn't mean the same design can accommodate a new way of calculating report totals. You design for a particular kind of change, not every change you can imagine.
Example: adding a report format
Suppose an exporter gathers report data and then uses a switch to render it as JSON or CSV. Every new format sends you back to the same function, even though the data-gathering step hasn't changed. If formats are added often, separate the part that changes from the part that stays the same. In this TypeScript example, collectRows is the existing data-gathering function:
type Row = { name: string; total: number };
type Formatter = (rows: Row[]) => string;
function exportReport(format: Formatter): string {
return format(collectRows());
}
const toJson: Formatter = rows => JSON.stringify(rows);
const report = exportReport(toJson);
To support CSV, write a toCsv function that accepts the same rows and returns CSV text, then pass it to exportReport. The exporter doesn't need to know which format it is producing. Something still has to choose the formatter: the caller can pass it directly, or the application can look it up in a registry. Updating that registry is not the same as rewriting the export workflow.
When the extra abstraction pays off
Look for changes that recur while the surrounding workflow stays stable, such as new delivery methods or discount policies. Give the changing part a contract small enough for implementations to share. A function parameter may be all you need; interfaces and plugins are other options. OCP does not require inheritance, forbid conditionals, or tell you to leave bugs unfixed.
If there are only two fixed cases, a switch may be the clearest implementation. Introducing an extension point too early adds indirection and can lock you into a contract before you know what future variants need. For example, the contract above assumes a text result; supporting a binary spreadsheet might require a different contract.
How it comes up in interviews
An interviewer may show you a growing conditional and ask how you'd add another case. Go beyond saying “use the Strategy pattern.” Name the likely change, show what stays untouched when the new variant is added, and explain where the variant is selected. If you'd keep the conditional, explain what makes the set of cases stable enough that an extension point isn't worth the cost.