Skip to content

Commit

Permalink
doc add code example for #123
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Aug 10, 2024
1 parent 5ea3f88 commit e4d6081
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/rules/rrd/magic-numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,28 @@ Magic numbers are hard-coded numeric values that appear directly in your code wi
- **Testability:** When magic numbers are replaced by named constants, it becomes easier to test the code. You can change the constant's value in one place, making it straightforward to test different scenarios without hunting down every instance of the magic number.
- **Maintainability:** If you need to update the value of a magic number, doing so across the codebase can be error-prone and tedious. By using named constants, you centralize the value, making maintenance easier and less risky.
- **Scalability:** As the project grows, more magic numbers can accumulate, making the code increasingly difficult to manage. Using named constants from the start ensures that the code remains scalable and that changes can be made efficiently.

## 😱 Examples of code for which this rule will throw a warning

::: warning
The following code contains magic numbers. What does 100 mean? Where it came from?
:::

```javascript
if (x > 100) {
// do something
}
```

## 🤩 How to fix it?

::: tip
Refactor the code to use named constants or variables instead of magic numbers.
:::

```javascript
const MAX_CAPACITY = 100
if (x > MAX_CAPACITY) {
// do something
}
```

0 comments on commit e4d6081

Please sign in to comment.