Rules Hub
Coding Rules Library
Avoid generic variable names like 'data'
Variable names must describe the specific domain concept they hold. Generic terms like 'data', 'info', or 'items' fail to convey intent and make code harder to understand.
Bad example
| 1 | const data = await fetchUserOrders(); |
| 2 | const hasData = data.orders.length > 0; |
| 3 |
|
| 4 | if (hasData) { |
| 5 | process(data); |
| 6 | } |
Explanation (EN)
The names 'data' and 'hasData' are semantically empty because everything in software is data. They force the reader to check the assignment source to understand what strictly domain-specific information is being handled.
Objašnjenje (HR)
Imena 'data' i 'hasData' su semantički prazna jer je sve u softveru podatak. Ona prisiljavaju čitatelja da provjeri izvorni kod dodjele kako bi razumio o kojim se točno informacijama iz domene radi.
Good example
| 1 | const userOrders = await fetchUserOrders(); |
| 2 | const hasOrders = userOrders.orders.length > 0; |
| 3 |
|
| 4 | if (hasOrders) { |
| 5 | process(userOrders); |
| 6 | } |
Explanation (EN)
The variables are named after the specific business entities they contain ('userOrders', 'hasOrders'). This makes the code self-documenting and the logic obvious without tracing definitions.
Objašnjenje (HR)
Varijable su imenovane prema specifičnim poslovnim entitetima koje sadrže ('userOrders', 'hasOrders'). To čini kod samo-dokumentirajućim, a logiku očitom bez potrebe za praćenjem definicija.