lib/cc/analyzer/source_extractor.rb
Method to_offset
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
Open
def to_offset(line, column, offset = 0)
source.each_line.with_index do |source_line, index|
offset +=
if line == index
column
- 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
Prefer transform_values
over each_with_object
. Open
Open
positions.each_with_object({}) do |(key, value), memo|
memo[key] =
if value.key?("offset")
value
else
- Read upRead up
- Exclude checks
Looks for uses of _.each_with_object({}) {...}
,
_.map {...}.to_h
, and Hash[_.map {...}]
that are actually just
transforming the values of a hash, and tries to use a simpler & faster
call to transform_values
instead.
Safety:
This cop is unsafe, as it can produce false positives if we are
transforming an enumerable of key-value-like pairs that isn't actually
a hash, e.g.: [[k1, v1], [k2, v2], ...]
Example:
# bad
{a: 1, b: 2}.each_with_object({}) { |(k, v), h| h[k] = foo(v) }
Hash[{a: 1, b: 2}.collect { |k, v| [k, foo(v)] }]
{a: 1, b: 2}.map { |k, v| [k, v * v] }.to_h
{a: 1, b: 2}.to_h { |k, v| [k, v * v] }
# good
{a: 1, b: 2}.transform_values { |v| foo(v) }
{a: 1, b: 2}.transform_values { |v| v * v }