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).
Name date/time variables after the instant they represent, not the function that produced them
Give timezone/date variables names that state which zone or representation the value is in (utc/local/zoned), so the conversion direction is unambiguous.
Bad example
| 1 | // fromZonedTime() interprets the input as wall-clock time in `timezone` |
| 2 | // and returns the equivalent UTC instant. |
| 3 | const zonedTime = fromZonedTime(parsedDateTime, timezone); |
| 4 | return formatTz(zonedTime, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", { timeZone: timezone }); |
Explanation (EN)
The variable is called `zonedTime` but it holds the UTC result of the conversion. A reviewer reading this cannot tell whether they are looking at a local instant or a UTC instant, which is the one fact that matters in timezone code.
Objašnjenje (HR)
Varijabla se zove `zonedTime` ali drzi UTC rezultat konverzije. Recenzent koji ovo cita ne moze znati gleda li lokalni ili UTC trenutak, a upravo je to jedina stvar koja je bitna u kodu s vremenskim zonama.
Good example
| 1 | // fromZonedTime() interprets the input as wall-clock time in `timezone` |
| 2 | // and returns the equivalent UTC instant. |
| 3 | const utcTime = fromZonedTime(parsedDateTime, timezone); |
| 4 | return formatTz(utcTime, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", { timeZone: timezone }); |
Explanation (EN)
The name now states the representation (`utcTime`), so the next reader immediately knows the conversion direction and what instant the value encodes without tracing the library call.
Objašnjenje (HR)
Naziv sada govori reprezentaciju (`utcTime`), pa sljedeci citatelj odmah zna smjer konverzije i koji trenutak vrijednost predstavlja bez prolazenja kroz poziv biblioteke.
Notes (EN)
Applies to any conversion-heavy domain (currency, units, encodings): name the result by what it represents, not by the converter that produced it.
Bilješke (HR)
Vrijedi za bilo koju domenu s puno konverzija (valute, jedinice, enkodiranja): imenuj rezultat prema onome sto predstavlja, a ne prema konverteru koji ga je proizveo.