Rules Hub

Coding Rules Library

← Back to all rules
frontend ruleStack: general
clean-coderefactoringmaintainabilityreadabilitybest-practices

Replace magic values with named collection constants

Avoid hardcoding specific string or number literals in conditional logic. Use named arrays or Sets to define the business rule, making the code more readable and extensible.

PR: Feat/FCK-2295 - Hide number of billionaires on 400 richest #4159Created: Dec 7, 2025

Bad example

Old codetsx
1const FinancialReport = ({ year }: { year: string }) => {
2 // Bad: Magic string '2025' is hardcoded inline.
3 // It's unclear why this year is excluded, and hard to add more years later.
4 if (year === '2025' || year === '2024') {
5 return null;
6 }
7
8 return <div>Report Content</div>;
9};

Explanation (EN)

Hardcoding specific values like '2025' directly in the control flow makes the code brittle and the intent unclear. If the list of excluded years grows, the condition becomes messy and hard to read.

Objašnjenje (HR)

Hardkodiranje specifičnih vrijednosti poput '2025' izravno u kontrolni tok čini kod krhkim i nejasnim. Ako se lista isključenih godina poveća, uvjet postaje neuredan i težak za čitanje.

Good example

New codetsx
1// Good: The business logic is extracted into a named constant.
2const EXCLUDED_REPORT_YEARS = ['2025', '2024'];
3
4const FinancialReport = ({ year }: { year: string }) => {
5 // The condition is now declarative and easy to extend.
6 if (EXCLUDED_REPORT_YEARS.includes(year)) {
7 return null;
8 }
9
10 return <div>Report Content</div>;
11};

Explanation (EN)

Extracting the values into a named constant (`EXCLUDED_REPORT_YEARS`) separates the configuration from the logic. This makes the component easier to read and allows you to modify the business rules without touching the render logic.

Objašnjenje (HR)

Izdvajanje vrijednosti u imenovanu konstantu (`EXCLUDED_REPORT_YEARS`) odvaja konfiguraciju od logike. To čini komponentu lakšom za čitanje i omogućuje izmjenu poslovnih pravila bez diranja logike renderiranja.

Notes (EN)

For small lists, an array with `.includes()` is sufficient. For large datasets, consider using a `Set` for O(1) lookup performance.

Bilješke (HR)

Za male liste dovoljan je niz s `.includes()`. Za velike skupove podataka razmislite o korištenju `Set` strukture za O(1) performanse pretraživanja.