Assignment Branch Condition size for advance is too high. [54.48/15] Open
def advance(step, n = 0, total = nil, bin = true)
# Initialize advance timing
@_advance_time ||= { last: nil, n: 0, avg: nil }
if @_advance_time[:n] > n
@_advance_time[:last] = nil
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
Method has too many lines. [37/10] Open
def advance(step, n = 0, total = nil, bin = true)
# Initialize advance timing
@_advance_time ||= { last: nil, n: 0, avg: nil }
if @_advance_time[:n] > n
@_advance_time[:last] = nil
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Perceived complexity for advance is too high. [16/7] Open
def advance(step, n = 0, total = nil, bin = true)
# Initialize advance timing
@_advance_time ||= { last: nil, n: 0, avg: nil }
if @_advance_time[:n] > n
@_advance_time[:last] = nil
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop tries to produce a complexity score that's a measure of the
complexity the reader experiences when looking at a method. For that
reason it considers when
nodes as something that doesn't add as much
complexity as an if
or a &&
. Except if it's one of those special
case
/when
constructs where there's no expression after case
. Then
the cop treats it as an if
/elsif
/elsif
... and lets all the when
nodes count. In contrast to the CyclomaticComplexity cop, this cop
considers else
nodes as adding complexity.
Example:
def my_method # 1
if cond # 1
case var # 2 (0.8 + 4 * 0.2, rounded)
when 1 then func_one
when 2 then func_two
when 3 then func_three
when 4..10 then func_other
end
else # 1
do_something until a && b # 2
end # ===
end # 7 complexity points
Method advance
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
def advance(step, n = 0, total = nil, bin = true)
# Initialize advance timing
@_advance_time ||= { last: nil, n: 0, avg: nil }
if @_advance_time[:n] > n
@_advance_time[:last] = nil
- Read upRead up
- Create a ticketCreate a ticket
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
Cyclomatic complexity for advance is too high. [13/6] Open
def advance(step, n = 0, total = nil, bin = true)
# Initialize advance timing
@_advance_time ||= { last: nil, n: 0, avg: nil }
if @_advance_time[:n] > n
@_advance_time[:last] = nil
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop 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.
Method advance
has 37 lines of code (exceeds 25 allowed). Consider refactoring. Open
def advance(step, n = 0, total = nil, bin = true)
# Initialize advance timing
@_advance_time ||= { last: nil, n: 0, avg: nil }
if @_advance_time[:n] > n
@_advance_time[:last] = nil
- Create a ticketCreate a ticket
Method num_suffix
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
def num_suffix(n, bin = false)
p = ''
{ T: 4, G: 3, M: 2, K: 1 }.each do |k, x|
v = (bin ? 1024 : 1e3)**x
if n > v
- Read upRead up
- Create a ticketCreate a ticket
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
Avoid more than 3 levels of block nesting. Open
left_time > 1440 ? ('%.1fd left' % (left_time / 1440)) :
left_time > 60 ? ('%.1fh left' % (left_time / 60)) :
('%.1fm left' % left_time)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for excessive nesting of conditional and looping constructs.
You can configure if blocks are considered using the CountBlocks
option. When set to false
(the default) blocks are not counted
towards the nesting level. Set to true
to count blocks as well.
The maximum level of nesting allowed is configurable.
Avoid multi-line ternary operators, use if
or unless
instead. Open
left_time < 1 ? ('%.0fs left' % (left_time * 60)) :
left_time > 1440 ? ('%.1fd left' % (left_time / 1440)) :
left_time > 60 ? ('%.1fh left' % (left_time / 60)) :
('%.1fm left' % left_time)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for multi-line ternary op expressions.
Example:
# bad
a = cond ?
b : c
a = cond ? b :
c
a = cond ?
b :
c
# good
a = cond ? b : c
a =
if cond
b
else
c
end
Ternary operators must not be nested. Prefer if
or else
constructs instead. Open
left_time > 1440 ? ('%.1fd left' % (left_time / 1440)) :
left_time > 60 ? ('%.1fh left' % (left_time / 60)) :
('%.1fm left' % left_time)
- Create a ticketCreate a ticket
- Exclude checks
Avoid multi-line ternary operators, use if
or unless
instead. Open
left_time > 1440 ? ('%.1fd left' % (left_time / 1440)) :
left_time > 60 ? ('%.1fh left' % (left_time / 60)) :
('%.1fm left' % left_time)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for multi-line ternary op expressions.
Example:
# bad
a = cond ?
b : c
a = cond ? b :
c
a = cond ?
b :
c
# good
a = cond ? b : c
a =
if cond
b
else
c
end
Avoid multi-line ternary operators, use if
or unless
instead. Open
left_time > 60 ? ('%.1fh left' % (left_time / 60)) :
('%.1fm left' % left_time)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for multi-line ternary op expressions.
Example:
# bad
a = cond ?
b : c
a = cond ? b :
c
a = cond ?
b :
c
# good
a = cond ? b : c
a =
if cond
b
else
c
end
Ternary operators must not be nested. Prefer if
or else
constructs instead. Open
left_time < 1 ? ('%.0fs left' % (left_time * 60)) :
left_time > 1440 ? ('%.1fd left' % (left_time / 1440)) :
left_time > 60 ? ('%.1fh left' % (left_time / 60)) :
('%.1fm left' % left_time)
- Create a ticketCreate a ticket
- Exclude checks
Prefer annotated tokens (like %<foo>s</foo>
) over unannotated tokens (like %s
). Open
$stderr.print("[%s] %s %s %s \r" % [Time.now, step, adv, left])
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Use a consistent style for named format string tokens.
Note:
unannotated
style cop only works for strings
which are passed as arguments to those methods:
sprintf
, format
, %
.
The reason is that unannotated format is very similar
to encoded URLs or Date/Time formatting strings.
Example: EnforcedStyle: annotated (default)
# bad
format('%{greeting}', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%<greeting>s', greeting: 'Hello')</greeting>
Example: EnforcedStyle: template
# bad
format('%<greeting>s', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%{greeting}', greeting: 'Hello')</greeting>
Example: EnforcedStyle: unannotated
# bad
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', 'Hello')
# good
format('%s', 'Hello')</greeting>
Prefer annotated tokens (like %<foo>s</foo>
) over unannotated tokens (like %s
). Open
('%.1f%% (%s/%s)' % vals)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Use a consistent style for named format string tokens.
Note:
unannotated
style cop only works for strings
which are passed as arguments to those methods:
sprintf
, format
, %
.
The reason is that unannotated format is very similar
to encoded URLs or Date/Time formatting strings.
Example: EnforcedStyle: annotated (default)
# bad
format('%{greeting}', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%<greeting>s', greeting: 'Hello')</greeting>
Example: EnforcedStyle: template
# bad
format('%<greeting>s', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%{greeting}', greeting: 'Hello')</greeting>
Example: EnforcedStyle: unannotated
# bad
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', 'Hello')
# good
format('%s', 'Hello')</greeting>
Prefer annotated tokens (like %<foo>s</foo>
) over unannotated tokens (like %s
). Open
$stderr.print("[%s] %s %s %s \r" % [Time.now, step, adv, left])
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Use a consistent style for named format string tokens.
Note:
unannotated
style cop only works for strings
which are passed as arguments to those methods:
sprintf
, format
, %
.
The reason is that unannotated format is very similar
to encoded URLs or Date/Time formatting strings.
Example: EnforcedStyle: annotated (default)
# bad
format('%{greeting}', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%<greeting>s', greeting: 'Hello')</greeting>
Example: EnforcedStyle: template
# bad
format('%<greeting>s', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%{greeting}', greeting: 'Hello')</greeting>
Example: EnforcedStyle: unannotated
# bad
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', 'Hello')
# good
format('%s', 'Hello')</greeting>
Use n.zero?
instead of n == 0
. Open
(n == 0 ? '' : num_suffix(n, bin))
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for usage of comparison operators (==
,
>
, <
) to test numbers as zero, positive, or negative.
These can be replaced by their respective predicate methods.
The cop can also be configured to do the reverse.
The cop disregards #nonzero?
as it its value is truthy or falsey,
but not true
and false
, and thus not always interchangeable with
!= 0
.
The cop ignores comparisons to global variables, since they are often
populated with objects which can be compared with integers, but are
not themselves Interger
polymorphic.
Example: EnforcedStyle: predicate (default)
# bad
foo == 0
0 > foo
bar.baz > 0
# good
foo.zero?
foo.negative?
bar.baz.positive?
Example: EnforcedStyle: comparison
# bad
foo.zero?
foo.negative?
bar.baz.positive?
# good
foo == 0
0 > foo
bar.baz > 0
Use next
to skip iteration. Open
if n > v
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Use next
to skip iteration instead of a condition at the end.
Example: EnforcedStyle: skipmodifierifs (default)
# bad
[1, 2].each do |a|
if a == 1
puts a
end
end
# good
[1, 2].each do |a|
next unless a == 1
puts a
end
# good
[1, 2].each do |o|
puts o unless o == 1
end
Example: EnforcedStyle: always
# With `always` all conditions at the end of an iteration needs to be
# replaced by next - with `skip_modifier_ifs` the modifier if like
# this one are ignored: `[1, 2].each { |a| return 'yes' if a == 1 }`
# bad
[1, 2].each do |o|
puts o unless o == 1
end
# bad
[1, 2].each do |a|
if a == 1
puts a
end
end
# good
[1, 2].each do |a|
next unless a == 1
puts a
end
Avoid multi-line ternary operators, use if
or unless
instead. Open
left_time < 0.01 ? ' ' :
left_time < 1 ? ('%.0fs left' % (left_time * 60)) :
left_time > 1440 ? ('%.1fd left' % (left_time / 1440)) :
left_time > 60 ? ('%.1fh left' % (left_time / 60)) :
('%.1fm left' % left_time)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for multi-line ternary op expressions.
Example:
# bad
a = cond ?
b : c
a = cond ? b :
c
a = cond ?
b :
c
# good
a = cond ? b : c
a =
if cond
b
else
c
end
Ternary operators must not be nested. Prefer if
or else
constructs instead. Open
left_time > 60 ? ('%.1fh left' % (left_time / 60)) :
('%.1fm left' % left_time)
- Create a ticketCreate a ticket
- Exclude checks
Prefer annotated tokens (like %<foo>s</foo>
) over unannotated tokens (like %s
). Open
$stderr.print("[%s] %s %s %s \r" % [Time.now, step, adv, left])
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Use a consistent style for named format string tokens.
Note:
unannotated
style cop only works for strings
which are passed as arguments to those methods:
sprintf
, format
, %
.
The reason is that unannotated format is very similar
to encoded URLs or Date/Time formatting strings.
Example: EnforcedStyle: annotated (default)
# bad
format('%{greeting}', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%<greeting>s', greeting: 'Hello')</greeting>
Example: EnforcedStyle: template
# bad
format('%<greeting>s', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%{greeting}', greeting: 'Hello')</greeting>
Example: EnforcedStyle: unannotated
# bad
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', 'Hello')
# good
format('%s', 'Hello')</greeting>
Prefer annotated tokens (like %<foo>s</foo>
) over unannotated tokens (like %s
). Open
$stderr.print("[%s] %s %s %s \r" % [Time.now, step, adv, left])
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Use a consistent style for named format string tokens.
Note:
unannotated
style cop only works for strings
which are passed as arguments to those methods:
sprintf
, format
, %
.
The reason is that unannotated format is very similar
to encoded URLs or Date/Time formatting strings.
Example: EnforcedStyle: annotated (default)
# bad
format('%{greeting}', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%<greeting>s', greeting: 'Hello')</greeting>
Example: EnforcedStyle: template
# bad
format('%<greeting>s', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%{greeting}', greeting: 'Hello')</greeting>
Example: EnforcedStyle: unannotated
# bad
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', 'Hello')
# good
format('%s', 'Hello')</greeting>
Prefer annotated tokens (like %<foo>s</foo>
) over unannotated tokens (like %s
). Open
('%.1f%% (%s/%s)' % vals)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Use a consistent style for named format string tokens.
Note:
unannotated
style cop only works for strings
which are passed as arguments to those methods:
sprintf
, format
, %
.
The reason is that unannotated format is very similar
to encoded URLs or Date/Time formatting strings.
Example: EnforcedStyle: annotated (default)
# bad
format('%{greeting}', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%<greeting>s', greeting: 'Hello')</greeting>
Example: EnforcedStyle: template
# bad
format('%<greeting>s', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%{greeting}', greeting: 'Hello')</greeting>
Example: EnforcedStyle: unannotated
# bad
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', 'Hello')
# good
format('%s', 'Hello')</greeting>