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 logic duplicated across services into a shared helper
When two or more services contain near-identical computation, extract it into one shared function instead of keeping divergent copies.
Bad example
| 1 | class GraphService { |
| 2 | private computePriceMetrics(p: Portfolio, token: string) { |
| 3 | const active = p.instruments.filter((i) => i.quantity > 0); |
| 4 | const metrics = pricingService.fetchMetrics(active, token); |
| 5 | return derive(metrics); |
| 6 | } |
| 7 | } |
| 8 |
|
| 9 | class MetricsService { |
| 10 | // Same intent, slightly different code — the two copies will drift apart. |
| 11 | private computePriceMetrics(p: Portfolio, token: string) { |
| 12 | const active = p.instruments.filter((i) => i.quantity > 0); |
| 13 | const metrics = pricingService.fetchMetrics(active, token); |
| 14 | return derive(metrics); |
| 15 | } |
| 16 | } |
Explanation (EN)
The same computation is implemented twice in two services. The copies are already 'somewhat different' and will keep diverging, so a fix in one place silently fails to reach the other.
Objašnjenje (HR)
Isti izracun implementiran je dvaput u dva servisa. Kopije su vec 'donekle razlicite' i nastavit ce se razilaziti, pa popravak na jednom mjestu tiho ne stigne do drugog.
Good example
| 1 | // shared helper used by both services |
| 2 | export function computePriceMetrics(p: Portfolio, token: string) { |
| 3 | const active = p.instruments.filter((i) => i.quantity > 0); |
| 4 | const metrics = pricingService.fetchMetrics(active, token); |
| 5 | return derive(metrics); |
| 6 | } |
| 7 |
|
| 8 | class GraphService { |
| 9 | build(p: Portfolio, token: string) { |
| 10 | return computePriceMetrics(p, token); |
| 11 | } |
| 12 | } |
| 13 |
|
| 14 | class MetricsService { |
| 15 | build(p: Portfolio, token: string) { |
| 16 | return computePriceMetrics(p, token); |
| 17 | } |
| 18 | } |
Explanation (EN)
The shared logic lives in one place and both services call it, so there is a single source of truth and fixes apply everywhere at once.
Objašnjenje (HR)
Zajednicka logika nalazi se na jednom mjestu i oba je servisa pozivaju, pa postoji jedan izvor istine i ispravci vrijede svugdje odjednom.
Notes (EN)
Only consolidate when the two copies truly share intent. If the apparent duplication is coincidental and the rules will legitimately diverge, keeping them separate is better than a forced abstraction.
Bilješke (HR)
Konsolidiraj samo kada dvije kopije stvarno dijele namjeru. Ako je naizgledna duplikacija slucajna i pravila ce se legitimno razilaziti, bolje ih je drzati odvojeno nego prisilno apstrahirati.
Exceptions / Tradeoffs (EN)
Skip extraction for trivial one-liners or when sharing would couple two modules that should stay independent. Balance against avoid-premature-abstraction-over-small-repetition: extract only when the shared computation is non-trivial and identical across 2+ services; otherwise leave it inline. Balance against create-dedicated-transformation-methods-for-distinct-contexts: consolidate only when the logic is genuinely one concept; do not merge transforms that serve unrelated contexts.
Iznimke / Tradeoffi (HR)
Preskoci ekstrakciju za trivijalne jednolinijske isjecke ili kada bi dijeljenje povezalo dva modula koji bi trebali ostati neovisni.