Cyclomatic complexity for action_body is too high. [13/11] Open
def action_body(action_name, *args, &block)
args = args.flatten
args = args.first if args.size == 1 && args.first.kind_of?(Hash)
args = {} if args.blank?
block_data = block ? block.call : {}
- Read upRead up
- Exclude checks
Checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.
An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one. Blocks that are calls to builtin iteration methods (e.g. `ary.map{...}) also add one, others are ignored.
def each_child_node(*types) # count begins: 1
unless block_given? # unless: +1
return to_enum(__method__, *types)
children.each do |child| # each{}: +1
next unless child.is_a?(Node) # unless: +1
yield child if types.empty? || # if: +1, ||: +1
types.include?(child.type)
end
self
end # total: 6
Method action_body
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring. Open
def action_body(action_name, *args, &block)
args = args.flatten
args = args.first if args.size == 1 && args.first.kind_of?(Hash)
args = {} if args.blank?
block_data = block ? block.call : {}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method find
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
def find(*args)
request_array = args.size == 1 && args[0].kind_of?(Array)
args = args.flatten
case args.size
when 0
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method parameters_from_query_relation
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
def parameters_from_query_relation(options)
api_params = {}
[:offset, :limit].each { |opt| api_params[opt] = options[opt] if options[opt] }
api_params[:attributes] = options[:select].join(",") if options[:select].present?
if options[:where]
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Use yield
instead of block.call
. Open
block_data = block ? block.call : {}
- Read upRead up
- Exclude checks
This cop identifies the use of a &block
parameter and block.call
where yield
would do just as well.
Example:
# bad
def method(&block)
block.call
end
def another(&func)
func.call 1, 2, 3
end
# good
def method
yield
end
def another
yield 1, 2, 3
end