- Home
- Skills
- Yanko Belov
- Code Craft
- Open Closed
open-closed_skill
- TypeScript
6
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill yanko-belov/code-craft --skill open-closed- SKILL.md5.9 KB
Overview
This skill enforces the Open/Closed Principle when adding new functionality to existing TypeScript code. It guides developers to extend behavior via interfaces and implementations instead of modifying working code. Use it to avoid growing if/else or switch chains and to keep systems maintainable.
How this skill works
The skill detects situations where new behavior is being added by branching (else if / case) and recommends the strategy/plugin pattern: define an interface, implement new variants, and register or inject them. It provides concrete refactoring steps and a pressure-resistance protocol for common objections. Examples show how a processor remains unchanged while new implementations are added.
When to use it
- Adding a new payment method, notification channel, export format, or discount type
- When you're tempted to add another if/else branch or switch case
- Extending behavior of an existing class without changing its core logic
- When requested to support a new variant and existing code already handles similar variants
- When touching code that already has multiple type-based branches
Best practices
- Never add another branch to an existing if/else or switch: extract an interface instead
- Create a small interface (e.g., PaymentMethod) and one implementation per variant
- Register or inject implementations so processors remain unchanged when adding variants
- Refactor on touch: convert existing branches to implementations when adding a new case
- Prefer composition and registration over central conditional logic for extensibility
Example use cases
- Add Apple Pay: implement ApplePayPayment and register it with the payment processor
- Add Slack notifications: create SlackNotifier implementing Notifier and inject it
- Add XLSX export: build XlsxExporter implementing Exporter and plug it into the exporter registry
- Convert a 5-branch discount function into DiscountStrategy implementations
- Refactor an existing switch-based router into route handler classes registered dynamically
FAQ
Refactor now: extract an interface from the branches, implement each branch as a class, and replace the conditional with a registry or factory. Existing violations aren't an excuse to add more.
Is strategy/plugin overkill for a single extra case?
No. Adding one case now tends to become many. Implementing the pattern prevents exponential maintenance cost and usually takes the same time as writing another branch.