Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP1universalStack: typescript
refactoringpolymorphismdesignmaintainability
Replace repeated type-switch conditionals with polymorphism
When the same value is switched on in multiple places to vary behavior, model it with subtypes/strategy objects instead of duplicated conditionals.
PR: frontpage-web · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetypescript
| 1 | function getContent(type: ContextType) { |
| 2 | switch (type) { |
| 3 | case ContextType.A: return buildA(); |
| 4 | case ContextType.B: return buildB(); |
| 5 | } |
| 6 | } |
| 7 | function getParams(type: ContextType) { |
| 8 | switch (type) { |
| 9 | case ContextType.A: return paramsA(); |
| 10 | case ContextType.B: return paramsB(); |
| 11 | } |
| 12 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | interface ContentStrategy { |
| 2 | getContent(): Content; |
| 3 | getParams(): Params; |
| 4 | } |
| 5 | class AStrategy implements ContentStrategy { /* ... */ } |
| 6 | class BStrategy implements ContentStrategy { /* ... */ } |
| 7 |
|
| 8 | const strategy = createStrategy(type); // single conditional at construction |
| 9 | strategy.getContent(); |
| 10 | strategy.getParams(); |
Explanation (EN)
Objašnjenje (HR)