From 2c5b1056a98dbf390f51dd59abe037d867356a6f Mon Sep 17 00:00:00 2001 From: Issy Long Date: Thu, 24 Apr 2025 12:14:15 +0100 Subject: [PATCH] Seeing `unless !thing` breaks my brain - If not not thing, so it's actually yes thing! - Enable the RuboCop rule and add justification to the style guide. --- STYLEGUIDE.md | 17 +++++++++++++++++ config/default.yml | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/STYLEGUIDE.md b/STYLEGUIDE.md index 6cb942d1..31f33664 100644 --- a/STYLEGUIDE.md +++ b/STYLEGUIDE.md @@ -3,6 +3,7 @@ This is GitHub's Ruby Style Guide, inspired by [RuboCop's guide][rubocop-guide]. ## Table of Contents + 1. [Layout](#layout) 1. [Indentation](#indentation) 2. [Inline](#inline) @@ -763,6 +764,22 @@ if x > 10 end ``` +* Don't use `unless` with a negated condition. + [[link](#no-unless-negation)] + * RuboCop rule: Style/NegatedUnless + +```ruby +# bad +unless !condition? + do_something +end + +# good +if condition? + do_something +end +``` + ### Ternary operator * Avoid the ternary operator (`?:`) except in cases where all expressions are extremely diff --git a/config/default.yml b/config/default.yml index cf5d193e..115d24d6 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1370,7 +1370,7 @@ Style/NegatedIfElseCondition: Enabled: false Style/NegatedUnless: - Enabled: false + Enabled: true Style/NegatedWhile: Enabled: false