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).
Reference static members by class name, not `this`
In static methods, access static fields via the class name to avoid `this` ambiguity and improve readability.
Bad example
| 1 | class Service { |
| 2 | private static _instance: Service; |
| 3 | static getInstance(): Service { |
| 4 | if (!this._instance) this._instance = new Service(); |
| 5 | return this._instance; |
| 6 | } |
| 7 | } |
Explanation (EN)
Inside a static method `this` refers to the class itself, which works but is easy to misread and breaks if the method is ever detached or the static is accessed from a subclass context.
Objašnjenje (HR)
Unutar staticke metode `this` se odnosi na samu klasu, sto radi, ali se lako krivo cita i puca ako se metoda odvoji ili se statik pristupa iz konteksta podklase.
Good example
| 1 | class Service { |
| 2 | private static _instance: Service; |
| 3 | static getInstance(): Service { |
| 4 | if (!Service._instance) Service._instance = new Service(); |
| 5 | return Service._instance; |
| 6 | } |
| 7 | } |
Explanation (EN)
Naming the class explicitly makes it unambiguous that a static member is being accessed and reads consistently regardless of call context.
Objašnjenje (HR)
Eksplicitno imenovanje klase cini nedvosmislenim da se pristupa statickom clanu i cita se konzistentno bez obzira na kontekst poziva.