Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Inject a data provider behind an interface instead of branching on a flag
Don't nest if/else over a flag to choose how to fetch data — define a common provider interface, inject the chosen implementation, and keep route logic decoupled from the data source.
Bad example
| 1 | async function getFeed(params: FeedParams) { |
| 2 | if (config.useRedis) { |
| 3 | return redisProvider.feed(params); |
| 4 | } else if (config.useCache) { |
| 5 | return cacheProvider.feed(params); |
| 6 | } else { |
| 7 | return esProvider.feed(params); |
| 8 | } |
| 9 | } |
Explanation (EN)
The handler hardcodes knowledge of every data source and grows another branch each time a source is added. Logic and data-access concerns are tangled together.
Objašnjenje (HR)
Handler tvrdo kodira poznavanje svakog izvora podataka i dobiva novu granu svaki put kad se doda izvor. Logika i pristup podacima su isprepleteni.
Good example
| 1 | interface IFeedProvider { |
| 2 | feed(params: FeedParams): Promise<FeedResult>; |
| 3 | } |
| 4 |
|
| 5 | class FeedRepository { |
| 6 | constructor(private readonly provider: IFeedProvider) {} |
| 7 | getFeed(params: FeedParams) { |
| 8 | return this.provider.feed(params); // no branching, no source knowledge |
| 9 | } |
| 10 | } |
| 11 |
|
| 12 | // factory picks the implementation once, at wiring time |
| 13 | const provider: IFeedProvider = config.useRedis ? redisProvider : esProvider; |
| 14 | const repo = new FeedRepository(provider); |
Explanation (EN)
The repository depends only on the IFeedProvider interface. Adding a new source means writing a new provider and adjusting the factory — the route logic never changes.
Objašnjenje (HR)
Repozitorij ovisi samo o IFeedProvider sučelju. Dodavanje novog izvora znači pisanje novog providera i prilagodbu factoryja — logika rute se nikad ne mijenja.
Notes (EN)
Selecting the implementation in one factory keeps the flag check in a single place instead of duplicated across every call site.
Bilješke (HR)
Odabir implementacije u jednom factoryju drži provjeru flaga na jednom mjestu umjesto duplicirano na svakom pozivnom mjestu.
Exceptions / Tradeoffs (EN)
Balance against avoid-premature-abstraction-over-small-repetition: define a provider interface when fetch paths are non-trivial or will grow; a flag branch invites scattered drift.