From c14c55bdeb68d23a536cdbf729f1c71124b94764 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Mon, 18 Jan 2021 00:37:17 +0000 Subject: [PATCH] Add Prefer Positive Over Negative --- README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/README.md b/README.md index 420237a1..d2ca6aed 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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.