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).
Preserve still-valid sibling data when falling back for a missing field
A fallback triggered by one missing field shouldn't also drop other fields that are still available.
Bad example
| 1 | function buildImageData(asset: Asset) { |
| 2 | if (!asset.renditions?.highRes?.uri) { |
| 3 | // rendition missing, so we bail out completely — but options.width/height/aoi are still there! |
| 4 | return { url: asset.fallbackSrc, width: null, height: null, aoi: null }; |
| 5 | } |
| 6 | return { |
| 7 | url: asset.renditions.highRes.uri, |
| 8 | width: asset.options.width, |
| 9 | height: asset.options.height, |
| 10 | aoi: asset.options.aoi, |
| 11 | }; |
| 12 | } |
Explanation (EN)
Only the URL gets a fallback; width, height and aoi are unconditionally nulled even though `asset.options` (a sibling of the missing `renditions`) is usually still populated. Downstream consumers silently lose the editor's chosen crop/focal point.
Objašnjenje (HR)
Samo URL dobiva zamjensku vrijednost; width, height i aoi se bezuvjetno postavljaju na null iako `asset.options` (susjedno svojstvo uz nedostajući `renditions`) obično i dalje postoji. Kôd koji dalje koristi te podatke tiho gubi kadriranje/fokusnu točku koju je urednik odabrao.
Good example
| 1 | function buildImageData(asset: Asset) { |
| 2 | if (!asset.renditions?.highRes?.uri) { |
| 3 | // rendition missing, but other fields are still usable — keep them |
| 4 | return { |
| 5 | url: asset.fallbackSrc, |
| 6 | width: asset.options?.width ?? null, |
| 7 | height: asset.options?.height ?? null, |
| 8 | aoi: asset.options?.aoi ?? null, |
| 9 | }; |
| 10 | } |
| 11 | return { |
| 12 | url: asset.renditions.highRes.uri, |
| 13 | width: asset.options.width, |
| 14 | height: asset.options.height, |
| 15 | aoi: asset.options.aoi, |
| 16 | }; |
| 17 | } |
Explanation (EN)
The fallback branch only replaces the piece of data that's actually missing (the URL) and keeps pulling the rest from data that's still present, so the caller loses the least amount of information possible.
Objašnjenje (HR)
Zamjenska grana mijenja samo onaj dio podataka koji stvarno nedostaje (URL), a ostatak i dalje čita iz podataka koji postoje, pa pozivatelj gubi najmanju moguću količinu informacija.