From 91a75f2952f2f53743697f298918157ae45a3051 Mon Sep 17 00:00:00 2001 From: Yauheni Dakuka Date: Sun, 15 Feb 2026 14:52:54 +0400 Subject: [PATCH] Add guideline for minimizing negations in conditionals --- README.adoc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.adoc b/README.adoc index cb5b0333..cb3e505a 100644 --- a/README.adoc +++ b/README.adoc @@ -1736,6 +1736,32 @@ else end ---- +=== Minimize Negations in Conditionals [[minimize-negations]] + +Prefer the conditional form (`if` or `unless`) that results in fewer negations. +When negated terms outnumber positive terms in a condition, flip the keyword and invert the condition. + +[source,ruby] +---- +# bad +do_something if !foo && !bar + +# good +do_something unless foo || bar + +# bad +do_something unless !foo && !bar + +# good +do_something if foo || bar + +# bad +do_something unless x != 1 && y != 2 + +# good +do_something if x == 1 || y == 2 +---- + === Parentheses around Condition [[no-parens-around-condition]] Don't use parentheses around the condition of a control expression.