Rules Hub
Coding Rules Library
Restrict boolean prefixes to boolean values
Variables named with boolean prefixes (is, has, should) must strictly hold boolean values to ensure code clarity.
Bad example
| 1 | // 'hasLabel' sounds like a boolean, but holds a string |
| 2 | const hasLabel = data.labels?.[0]; |
| 3 |
|
| 4 | // 'tickerCategory' is ambiguous: is it a category OF tickers? |
| 5 | const tickerCategory = currentCategory || (hasLabel && { label: hasLabel }); |
| 6 |
|
| 7 | if (hasLabel) { // Works in JS, but confusing to read |
| 8 | console.log(hasLabel.toUpperCase()); |
| 9 | } |
Explanation (EN)
The variable `hasLabel` uses the `has` prefix, which conventionally indicates a boolean, but it holds a string. This mismatch creates confusion about the variable's type. Additionally, `tickerCategory` describes where the data is used (ticker) rather than what the data is (article category), making it misleading.
Objašnjenje (HR)
Varijabla `hasLabel` koristi prefiks `has`, koji konvencionalno označava boolean, ali sadrži string. Ovo nepodudaranje stvara zbrku oko tipa varijable. Također, `tickerCategory` opisuje gdje se podatak koristi (ticker), a ne što podatak zapravo jest (kategorija članka), što navodi na krivi zaključak.
Good example
| 1 | // Noun for the value, Boolean for the check |
| 2 | const labelName = data.labels?.[0]; |
| 3 | const hasLabel = Boolean(labelName); |
| 4 |
|
| 5 | // Name describes the entity (Article Category), not the UI component |
| 6 | const articleCategory = currentCategory || (labelName ? { label: labelName } : undefined); |
| 7 |
|
| 8 | if (hasLabel) { |
| 9 | console.log(labelName.toUpperCase()); |
| 10 | } |
Explanation (EN)
The string value is assigned to a noun-based variable (`labelName`), and the boolean check is explicit (`hasLabel`). The configuration object is renamed to `articleCategory` to accurately reflect the domain entity it represents, regardless of where it is rendered.
Objašnjenje (HR)
String vrijednost je dodijeljena imenici (`labelName`), a booleova provjera je eksplicitna (`hasLabel`). Konfiguracijski objekt je preimenovan u `articleCategory` kako bi točno odražavao domenski entitet koji predstavlja, bez obzira na to gdje se prikazuje.