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).
Extract duplicated request-parsing/validation logic into a shared helper on the second copy
Once the same parsing/validation block (e.g. pagination clamping) has been copy-pasted into a second route handler, extract it into a single named helper — separate copies drift in how they coerce edge cases, and that drift is exactly how correctness bugs creep in.
Bad example
| 1 | // routeA.ts |
| 2 | const limit = Math.min(parseInt(q.limit, 10) || 10, 100); |
| 3 |
|
| 4 | // routeB.ts |
| 5 | const limit = Math.min(Number(q.limit) || 10, 100); // different coercion, same intent |
Explanation (EN)
Two independently-maintained copies of the same logic will diverge in subtle ways over time — here in which coercion function is used — and each copy needs its own bugfix when an edge case is found.
Objašnjenje (HR)
Dvije neovisno održavane kopije iste logike vremenom će suptilno divergirati — ovdje u tome koja se funkcija koristi za koerciju — i svaka kopija zahtijeva vlastiti popravak kad se pronađe rubni slučaj.
Good example
| 1 | // helpers/parsePagination.ts |
| 2 | export function parsePagination(query: Request['query'], maxLimit = 100) { |
| 3 | const limit = Math.min(parseInt(String(query.limit ?? ''), 10) || 10, maxLimit); |
| 4 | const offset = Math.max(0, parseInt(String(query.offset ?? ''), 10) || 0); |
| 5 | return { limit, offset }; |
| 6 | } |
Explanation (EN)
A single shared helper means one fix and one set of tests cover every route that paginates, instead of N independently-drifting copies.
Objašnjenje (HR)
Jedan zajednički helper znači da jedan popravak i jedan set testova pokrivaju sve rute s paginacijom, umjesto N neovisno divergirajućih kopija.