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).
Compute 'now'/'today' at execution time, not at module load
Don't capture time-sensitive values like the current date at import/module-load time. Compute them inside the function that runs the work so they reflect the actual run moment.
Bad example
| 1 | // Evaluated once, when the module is imported |
| 2 | const date = new Date().toISOString().slice(0, 10); |
| 3 |
|
| 4 | runJob('sync-by-date', async (app) => { |
| 5 | await service.fetchByDate(date); // may be yesterday's date if the process crosses midnight |
| 6 | }); |
Explanation (EN)
The date is frozen at import time. For a scheduled or long-lived process the captured value can be stale when the job actually executes — e.g. the job starts at 23:59:59 and the date is off by a day.
Objašnjenje (HR)
Datum je zamrznut u trenutku importa. Za zakazani ili dugovječni proces zarobljena vrijednost može biti zastarjela kad se posao stvarno izvrši — npr. posao kreće u 23:59:59 i datum je pogrešan za jedan dan.
Good example
| 1 | runJob('sync-by-date', async (app) => { |
| 2 | // Computed when the job runs |
| 3 | const date = new Date().toISOString().slice(0, 10); |
| 4 | await service.fetchByDate(date); |
| 5 | }); |
Explanation (EN)
Reading the clock inside the job body guarantees the value reflects the moment the work runs, not the moment the module was loaded.
Objašnjenje (HR)
Čitanje sata unutar tijela posla jamči da vrijednost odražava trenutak izvršavanja posla, a ne trenutak učitavanja modula.
Notes (EN)
Same principle applies to any computed-at-import constant that depends on mutable external state (current time, env that may change, random seeds). Keep module top-level free of stateful side effects.
Bilješke (HR)
Isto načelo vrijedi za svaku konstantu izračunatu pri importu koja ovisi o promjenjivom vanjskom stanju (trenutno vrijeme, env koji se može promijeniti, random seedovi). Drži vrh modula slobodnim od side-effecta sa stanjem.