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).
Hoist a shared precondition to the dispatching method
Check a guard that gates the whole operation once at the orchestrator entry point, not duplicated in each branch method it dispatches to.
Bad example
| 1 | async handleArticleReplication(id, previous, next): Promise<void> { |
| 2 | const action = this.syncThreadState(previous?.allowArticleComments ?? null, next.allowArticleComments); |
| 3 | if (action === 'create') await this.createThread(id, next); |
| 4 | else if (action === 'close') await this.closeThread(id); |
| 5 | } |
| 6 |
|
| 7 | async createThread(id, article): Promise<void> { |
| 8 | if (!this.config.isHookEnabled) return; // duplicated |
| 9 | // ... |
| 10 | } |
| 11 |
|
| 12 | async closeThread(id): Promise<void> { |
| 13 | if (!this.config.isHookEnabled) return; // duplicated |
| 14 | // ... |
| 15 | } |
Explanation (EN)
Repeating the same enable-check in every branch method duplicates logic, risks the branches drifting out of sync, and forces every new branch to remember the guard.
Objašnjenje (HR)
Ponavljanje iste provjere ukljucenosti u svakoj metodi grane duplicira logiku, riskira da grane razidu, i prisiljava svaku novu granu da se sjeti zastite.
Good example
| 1 | async handleArticleReplication(id, previous, next): Promise<void> { |
| 2 | if (!this.config.isHookEnabled) { |
| 3 | this.logger.log(`[forum] skipped for ${id} (hook disabled)`); |
| 4 | return; |
| 5 | } |
| 6 | const action = this.syncThreadState(previous?.allowArticleComments ?? null, next.allowArticleComments); |
| 7 | if (action === 'create') await this.createThread(id, next); |
| 8 | else if (action === 'close') await this.closeThread(id); |
| 9 | } |
| 10 |
|
| 11 | async createThread(id, article): Promise<void> { |
| 12 | // no guard needed; orchestrator already gated |
| 13 | } |
Explanation (EN)
The precondition lives once at the single entry point that dispatches to the branches, so the guard cannot drift and new branches inherit it for free.
Objašnjenje (HR)
Preduvjet zivi jednom na jedinstvenoj ulaznoj tocki koja salje na grane, pa zastita ne moze razici i nove grane je nasljeduju besplatno.
Notes (EN)
Keep branch-specific preconditions (e.g. a missing base URL) inside the branch; only hoist the guard that applies to the whole operation.
Bilješke (HR)
Zadrzi preduvjete specificne za granu (npr. nedostajuci osnovni URL) unutar grane; hoistaj samo zastitu koja vrijedi za cijelu operaciju.
Exceptions / Tradeoffs (EN)
If only one branch is gated by the condition, leave the check in that branch rather than hoisting. Balance against enforce-function-level-authorization: hoist only non-authorization preconditions; never centralize the authz check that each endpoint must own.
Iznimke / Tradeoffi (HR)
Ako je samo jedna grana uvjetovana, ostavi provjeru u toj grani umjesto da je hoistas.