---
title: "Extend code without editing it · Agent Wranglers"
url: https://agentwranglers.se/playbook/architecture/solid-and-dry/open-closed
updated: 2026-10-20
licence: "All rights reserved"
---

# Extend code without editing it

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Recommended
- Chapter: [SOLID and DRY](https://agentwranglers.se/playbook/architecture/solid-and-dry)
- Rule: Add new behaviour by adding a new type or strategy, not by editing a growing switch or if chain.
- How: When the same switch on a type appears in two places, replace it. Use one class or function per case behind a shared interface. A new case then means a new file, and tested code stays untouched.
- Check: In review, adding a new case touches no existing branch. A linter limit on cyclomatic complexity, such as ESLint `complexity`, flags long switch chains.

Example:

```python
# Bad: every new carrier edits this function
def shipping_cost(carrier, weight):
    if carrier == "postnord": return 49 + weight * 2
    if carrier == "dhl": return 79 + weight * 3

# Good: a new carrier is a new class
class PostNord:
    def cost(self, weight): return 49 + weight * 2

class Dhl:
    def cost(self, weight): return 79 + weight * 3
```

```ts
// Good: a new carrier is a new object, not an edit
interface Carrier { cost(weight: number): number }
const postNord: Carrier = { cost: weight => 49 + weight * 2 };
const dhl: Carrier = { cost: weight => 79 + weight * 3 };
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers architecture playbook: https://agentwranglers.se/playbook/architecture/solid-and-dry/open-closed`.

- Previous: [Give each module one reason to change](https://agentwranglers.se/playbook/architecture/solid-and-dry/single-responsibility)
- Next: [Keep the contract in every subtype](https://agentwranglers.se/playbook/architecture/solid-and-dry/liskov-substitution)
- [SOLID and DRY](https://agentwranglers.se/playbook/architecture/solid-and-dry)
