SOLID and DRY Recommended
Extend code without editing it
Add new behaviour by adding a new type or strategy, not by editing a growing switch or if chain.
How to do it
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.
Example
# 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
How to 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.
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.