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).
Wrap a client by composition when a subclass can only guard some inherited methods
Subclassing a client to add a key prefix, a tenant scope or a permission check guards only the methods you actually override; every other inherited method stays a public, unguarded escape hatch onto the same connection. Expose a small wrapper with just the methods you support instead.
Bad example
| 1 | class ScopedClient extends BaseClient { |
| 2 | get(key) { return super.get(this.prefix + key); } |
| 3 | set(key, v) { return super.set(this.prefix + key, v); } |
| 4 | del(key) { return super.del(this.prefix + key); } |
| 5 | // inherited and unprefixed: mget, mset, sAdd, sRem, queueRPush, ... |
| 6 | } |
Explanation (EN)
Any future call to an unoverridden method writes unprefixed keys onto exactly the shared instance the prefix exists to protect.
Objašnjenje (HR)
Svaki buduci poziv metode koja nije pregazena zapisuje kljuceve bez prefiksa upravo na dijeljenu instancu koju prefiks treba stititi.
Good example
| 1 | class ScopedClient { |
| 2 | constructor(private readonly client: BaseClient, private readonly prefix: string) {} |
| 3 |
|
| 4 | get(key: string) { return this.client.get(this.prefix + key); } |
| 5 | set(key: string, v: string, ttl: number) { return this.client.set(this.prefix + key, v, ttl); } |
| 6 | del(key: string) { return this.client.del(this.prefix + key); } |
| 7 | } |
Explanation (EN)
The unguarded surface is unreachable rather than merely discouraged, so the invariant cannot be bypassed by accident.
Objašnjenje (HR)
Nezasticena povrsina je nedostupna, a ne samo nepozeljna, pa se invarijanta ne moze zaobici slucajno.
Notes (EN)
Ask what happens when someone calls a method you did not think about. With inheritance the answer is it silently does the wrong thing; with composition it does not compile.
Bilješke (HR)
Pitaj se sto se dogodi kad netko pozove metodu na koju nisi mislio. Kod nasljedivanja odgovor je tiho radi krivu stvar; kod kompozicije se ne prevodi.
Exceptions / Tradeoffs (EN)
If inheritance has to stay for other reasons, at least mark the unoverridden members protected so they are not part of the public surface.
Iznimke / Tradeoffi (HR)
Ako nasljedivanje mora ostati zbog drugih razloga, barem oznaci nepregazene clanove kao protected da ne budu dio javne povrsine.