Class: RuboCop::Cop::Guardrails::NoGuardClauses
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Guardrails::NoGuardClauses
- Defined in:
- lib/rubocop/cop/guardrails/no_guard_clauses.rb
Overview
Discourages guard clauses (conditional early returns).
Guard clauses obscure the actual flow of a method. In short methods, prefer a conditional expression. In longer methods, one guard clause is tolerated at the very beginning.
The ‘MinMethodLength` option (default: 10) controls the threshold. Methods longer than this are allowed one leading guard clause.
Constant Summary collapse
- MSG =
'Avoid guard clauses. Prefer a conditional expression.'
Instance Method Summary collapse
- #on_def(node) ⇒ Object (also: #on_defs)
Instance Method Details
#on_def(node) ⇒ Object Also known as: on_defs
48 49 50 51 52 53 |
# File 'lib/rubocop/cop/guardrails/no_guard_clauses.rb', line 48 def on_def(node) guards = leading_guard_clauses(node) allowed = method_length(node) > min_method_length ? 1 : 0 guards.drop(allowed).each { |guard| add_offense(guard) } end |