Tighten six guard conditions via De Morgan / blank? / present? / include?#2707
Open
ericproulx wants to merge 1 commit intomasterfrom
Open
Tighten six guard conditions via De Morgan / blank? / present? / include?#2707ericproulx wants to merge 1 commit intomasterfrom
ericproulx wants to merge 1 commit intomasterfrom
Conversation
Danger ReportNo issues found. |
28a97e1 to
464ccd6
Compare
…ude?
Six spots where an `if`/`unless` clause used multiple negations
joined by `&&` or `||`, or `&.any?` on a Hash. Each rewrite expresses
the positive shape of the predicate and keeps `if` over `unless`
where natural. Same behaviour, same allocation profile (one form
even reuses a previously-recomputed `Hash#except` result).
* `Grape::API::Boolean.build` —
`val != true && val != false` → frozen `VALUES` constant +
`unless VALUES.include?(val)`.
* `Grape::Request#make_params` —
`routing_args&.any? { |k, _| k != :version && k != :route_info }` +
separate `routing_args.except(:version, :route_info)` two lines
later → compute `except` once, gate on `filtered.blank?`.
* `Grape::Validations::ParamsScope#check_incompatible_option_values`
(early guard) —
`return unless default && !default.is_a?(Proc)` →
`return if default.nil? || default.is_a?(Proc)`.
* `Grape::Validations::ParamsScope#check_incompatible_option_values`
(raise condition) —
`!Array(default).all? { |v| values.include?(v) }` →
`Array(default).any? { |v| !values.include?(v) }`.
* `Grape::Validations::Validators::Base#scrub` —
`unless value.respond_to?(:valid_encoding?) && !value.valid_encoding?`
→ `if !value.respond_to?(:valid_encoding?) || value.valid_encoding?`.
* `Grape::DSL::Routing#route` —
`route_options&.any?` → `route_options.present?`.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
464ccd6 to
abc0d71
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Six small condition rewrites in
lib/where anif/unlessclause used multiple negations joined by&&/||, or&.any?on a Hash. Each one expresses the positive shape of the predicate, keepsifoverunlesswhere natural, and changes nothing about behaviour or allocation profile (one rewrite even saves a redundantHash#exceptcall).The six
1.
lib/grape/api.rb—Grape::API::Boolean.build"Neither true nor false" → "not one of
[true, false]". Frozen constant avoids per-call array allocation;private_constantkeeps the new symbol offBoolean's public surface.2.
lib/grape/request.rb—Request#make_paramsThe block predicate
k != :version && k != :route_infois "not in the excluded set", whichHash#exceptsays directly. Bonus:exceptwas being called twice (once via theany?block's negative form and once on the next line) — thefilteredlocal computes it once and reuses it.if filtered.blank?reads positively (nil or empty hash → bail).3.
lib/grape/validations/params_scope.rb—check_incompatible_option_values(early guard)unless A && !B↔if !A || B. Switching toif+ named conditions reads as "skip if there's nothing to check or if it's a Proc."4.
lib/grape/validations/params_scope.rb—check_incompatible_option_values(raise condition)!array.all? { p(x) }↔array.any? { !p(x) }. Rewritten form reads as "raise if any default value isn't in the allowed set" — which is the actual contract.5.
lib/grape/validations/validators/base.rb—Base#scrubunless A && !B↔if !A || B. Removes theunless ... && !triple-negative.6.
lib/grape/dsl/routing.rb—Routing#routeFor nil-or-Hash inputs
&.any?and.present?are equivalent.present?reads as "if there's something there", drops the safe-nav noise, and aligns with theblank?/present?style already used elsewhere inlib/grape/(router.rb, endpoint.rb, validations.rb).Why these six
I scanned
lib/forif/unlessclauses with multiple negations connected by&&/||, plus&.any?patterns on collection-or-nil values. Six passed the bar of "rewrite makes the positive intent clearer without growing the line." A handful of others (length_validator.rbtriple!@x.nil? &&clauses, e.g.) were either already fine or wanted a bigger refactor than a De Morgan flip.Test plan
bundle exec rspec— full suite green (2,260 examples, 0 failures)bundle exec rubocop lib/— cleanBehavior-preserving by construction (each change is a boolean-algebra-equivalent rewrite); the existing test suite covers the call sites.
🤖 Generated with Claude Code