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).
Don't keep mutable per-request state on a singleton
Avoid storing per-request query/filter state on a shared singleton; build it locally or create a fresh instance per call.
Bad example
| 1 | @Injectable() |
| 2 | class SearchProvider { |
| 3 | private musts: Clause[] = []; |
| 4 | byId(id: number) { this.musts = []; this.musts.push({ term: { id } }); return this; } |
| 5 | // two concurrent requests share `this.musts` and corrupt each other |
| 6 | } |
Explanation (EN)
A singleton instance shared across requests stores filter state on the instance, so concurrent calls overwrite each other's clauses.
Objašnjenje (HR)
Singleton instanca dijeljena među zahtjevima sprema stanje filtera na instanci, pa konkurentni pozivi prepisuju jedni drugima klauzule.
Good example
| 1 | class SearchQuery { |
| 2 | private musts: Clause[] = []; |
| 3 | byId(id: number) { this.musts.push({ term: { id } }); return this; } |
| 4 | } |
| 5 |
|
| 6 | @Injectable() |
| 7 | class SearchProvider { |
| 8 | byId(id: number) { return new SearchQuery().byId(id); } // fresh state per call |
| 9 | } |
Explanation (EN)
Each call gets its own query object, so concurrent requests never share mutable state.
Objašnjenje (HR)
Svaki poziv dobiva vlastiti query objekt, pa konkurentni zahtjevi nikad ne dijele promjenjivo stanje.
Exceptions / Tradeoffs (EN)
Balance against reuse-singleton-services-not-per-call-instantiation: build locally only the per-request state; keep the stateless client itself a reused singleton.