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
immutabilityconstructorsencapsulationoop
Set object state in the constructor to keep objects immutable
If all fields can be provided at construction, set them there and avoid setters; this makes the object immutable and prevents accidental later mutation.
PR: frontpage-web · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetypescript
| 1 | class QueryBuilder { |
| 2 | setLimit(n: number) { this._limit = n; } |
| 3 | setOffset(n: number) { this._offset = n; } |
| 4 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | class QueryBuilder { |
| 2 | constructor(private readonly limit: number, private readonly offset: number) {} |
| 3 | } |
Explanation (EN)
Objašnjenje (HR)