Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Prefer Positive Over Negative #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [S-I-D](#s-i-d)
- [Avoid contractions](#avoid-contractions)
- [Avoid context duplication](#avoid-context-duplication)
- [Prefer positive over negative](#prefer-positive-over-negative)
- [Reflect the expected result](#reflect-the-expected-result)
- [Naming functions](#naming-functions)
- [A/HC/LC pattern](#ahclc-pattern)
Expand Down Expand Up @@ -105,6 +106,41 @@ class MenuItem {
}
```

## Prefer positive over negative

It's easier to have at most one negation when reading code, so:

```jsx
/* Bad */
const isNotOldEnough = age < 13
if (isNotOldEnough) {
// ...
} else {
// ...
}

if (!isNotOldEnough) {
// ...
} else {
// Wait this case is for the else of if !isNotOldEnough, is... If not not isNotOldEnough!
}


/* Good */
const isOldEnough = age >= 13
if (isOldEnough) {
// ...
} else {
// ...
}

if (!isOldEnough) {
// ...
} else {
// ...
}
```

## Reflect the expected result

A name should reflect the expected result.
Expand Down