TracksCli::TracksXmlBuilder#xml_for_predecessor is controlled by argument 'dependend' Open
dependend ? "<predecessor_dependencies><predecessor>#{predecessor}</predecessor></predecessor_dependencies>" : ""
- Read upRead up
- Exclude checks
Control Parameter
is a special case of Control Couple
Example
A simple example would be the "quoted" parameter in the following method:
def write(quoted)
if quoted
write_quoted @value
else
write_unquoted @value
end
end
Fixing those problems is out of the scope of this document but an easy solution could be to remove the "write" method alltogether and to move the calls to "writequoted" / "writeunquoted" in the initial caller of "write".
TracksCli::TracksXmlBuilder#build_todo_xml refers to 'todo' more than self (maybe move it to another class?) Open
xml_for_description(todo[:description]),
xml_for_project_id(todo[:project_id]),
xml_for_show_from(todo[:show_from]),
xml_for_notes(todo[:notes]),
xml_for_taglist(todo[:taglist]),
- Read upRead up
- Exclude checks
Feature Envy occurs when a code fragment references another object more often than it references itself, or when several clients do the same series of manipulations on a particular type of object.
Feature Envy reduces the code's ability to communicate intent: code that "belongs" on one class but which is located in another can be hard to find, and may upset the "System of Names" in the host class.
Feature Envy also affects the design's flexibility: A code fragment that is in the wrong class creates couplings that may not be natural within the application's domain, and creates a loss of cohesion in the unwilling host class.
Feature Envy often arises because it must manipulate other objects (usually its arguments) to get them into a useful form, and one force preventing them (the arguments) doing this themselves is that the common knowledge lives outside the arguments, or the arguments are of too basic a type to justify extending that type. Therefore there must be something which 'knows' about the contents or purposes of the arguments. That thing would have to be more than just a basic type, because the basic types are either containers which don't know about their contents, or they are single objects which can't capture their relationship with their fellows of the same type. So, this thing with the extra knowledge should be reified into a class, and the utility method will most likely belong there.
Example
Running Reek on:
class Warehouse
def sale_price(item)
(item.price - item.rebate) * @vat
end
end
would report:
Warehouse#total_price refers to item more than self (FeatureEnvy)
since this:
(item.price - item.rebate)
belongs to the Item class, not the Warehouse.
TracksCli::TracksXmlBuilder#xml_for_taglist calls 'tag.strip' 2 times Open
tags = tags.collect { |tag| "<tag><name>#{tag.strip}</name></tag>" unless tag.strip.empty? }.join('')
- Read upRead up
- Exclude checks
Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.
Reek implements a check for Duplicate Method Call.
Example
Here's a very much simplified and contrived example. The following method will report a warning:
def double_thing()
@other.thing + @other.thing
end
One quick approach to silence Reek would be to refactor the code thus:
def double_thing()
thing = @other.thing
thing + thing
end
A slightly different approach would be to replace all calls of double_thing
by calls to @other.double_thing
:
class Other
def double_thing()
thing + thing
end
end
The approach you take will depend on balancing other factors in your code.
TracksCli::TracksXmlBuilder has no descriptive comment Open
class TracksXmlBuilder
- Read upRead up
- Exclude checks
Classes and modules are the units of reuse and release. It is therefore considered good practice to annotate every class and module with a brief comment outlining its responsibilities.
Example
Given
class Dummy
# Do things...
end
Reek would emit the following warning:
test.rb -- 1 warning:
[1]:Dummy has no descriptive comment (IrresponsibleModule)
Fixing this is simple - just an explaining comment:
# The Dummy class is responsible for ...
class Dummy
# Do things...
end
TracksCli::TracksXmlBuilder#xml_for_notes doesn't depend on instance state (maybe move it to another class?) Open
def xml_for_notes(notes)
- Read upRead up
- Exclude checks
A Utility Function is any instance method that has no dependency on the state of the instance.
TracksCli::TracksXmlBuilder#xml_for_show_from performs a nil-check Open
show_from.nil? ? "" : "<show-from type=\"datetime\">#{Time.at(show_from).xmlschema}</show-from>"
- Read upRead up
- Exclude checks
A NilCheck
is a type check. Failures of NilCheck
violate the "tell, don't ask" principle.
Additionally, type checks often mask bigger problems in your source code like not using OOP and / or polymorphism when you should.
Example
Given
class Klass
def nil_checker(argument)
if argument.nil?
puts "argument isn't nil!"
end
end
end
Reek would emit the following warning:
test.rb -- 1 warning:
[3]:Klass#nil_checker performs a nil-check. (NilCheck)
TracksCli::TracksXmlBuilder#build_project_xml doesn't depend on instance state (maybe move it to another class?) Open
def build_project_xml(project)
- Read upRead up
- Exclude checks
A Utility Function is any instance method that has no dependency on the state of the instance.
TracksCli::TracksXmlBuilder#xml_for_taglist performs a nil-check Open
unless taglist.nil?
- Read upRead up
- Exclude checks
A NilCheck
is a type check. Failures of NilCheck
violate the "tell, don't ask" principle.
Additionally, type checks often mask bigger problems in your source code like not using OOP and / or polymorphism when you should.
Example
Given
class Klass
def nil_checker(argument)
if argument.nil?
puts "argument isn't nil!"
end
end
end
Reek would emit the following warning:
test.rb -- 1 warning:
[3]:Klass#nil_checker performs a nil-check. (NilCheck)
TracksCli::TracksXmlBuilder#xml_for_taglist doesn't depend on instance state (maybe move it to another class?) Open
def xml_for_taglist(taglist)
- Read upRead up
- Exclude checks
A Utility Function is any instance method that has no dependency on the state of the instance.
TracksCli::TracksXmlBuilder#xml_for_notes performs a nil-check Open
notes.nil? ? "" : "<notes>#{notes}</notes>"
- Read upRead up
- Exclude checks
A NilCheck
is a type check. Failures of NilCheck
violate the "tell, don't ask" principle.
Additionally, type checks often mask bigger problems in your source code like not using OOP and / or polymorphism when you should.
Example
Given
class Klass
def nil_checker(argument)
if argument.nil?
puts "argument isn't nil!"
end
end
end
Reek would emit the following warning:
test.rb -- 1 warning:
[3]:Klass#nil_checker performs a nil-check. (NilCheck)
TracksCli::TracksXmlBuilder#xml_for_context doesn't depend on instance state (maybe move it to another class?) Open
def xml_for_context(context_name, context_id)
- Read upRead up
- Exclude checks
A Utility Function is any instance method that has no dependency on the state of the instance.
TracksCli::TracksXmlBuilder#xml_for_show_from doesn't depend on instance state (maybe move it to another class?) Open
def xml_for_show_from(show_from)
- Read upRead up
- Exclude checks
A Utility Function is any instance method that has no dependency on the state of the instance.
Complex method TracksCli::TracksXmlBuilder#build_todo_xml (20.5) Open
def build_todo_xml(todo)
props = [
xml_for_description(todo[:description]),
xml_for_project_id(todo[:project_id]),
xml_for_show_from(todo[:show_from]),
- Read upRead up
- Exclude checks
Flog calculates the ABC score for methods. The ABC score is based on assignments, branches (method calls), and conditions.
You can read more about ABC metrics or the flog tool
Use context_name.present?
instead of context_name && !context_name.empty?
. Open
if context_name && !context_name.empty?
- Read upRead up
- Exclude checks
This cops checks for code that can be changed to blank?
.
Settings:
NotNilAndNotEmpty: Convert checks for not nil
and not empty?
to present?
NotBlank: Convert usages of not blank?
to present?
UnlessBlank: Convert usages of unless
blank?
to if
present?
Example:
# NotNilAndNotEmpty: true
# bad
!foo.nil? && !foo.empty?
foo != nil && !foo.empty?
!foo.blank?
# good
foo.present?
# NotBlank: true
# bad
!foo.blank?
not foo.blank?
# good
foo.present?
# UnlessBlank: true
# bad
something unless foo.blank?
# good
something if foo.present?
Use a guard clause instead of wrapping the code inside a conditional expression. Open
if context_name && !context_name.empty?
- Read upRead up
- Exclude checks
Use a guard clause instead of wrapping the code inside a conditional expression
Example:
# bad
def test
if something
work
end
end
# good
def test
return unless something
work
end
# also good
def test
work if something
end
# bad
if something
raise 'exception'
else
ok
end
# good
raise 'exception' if something
ok
Do not use Time.at
without zone. Use one of Time.zone.at
, Time.current
, Time.at.in_time_zone
, Time.at.utc
, Time.at.getlocal
, Time.at.iso8601
, Time.at.jisx0301
, Time.at.rfc3339
, Time.at.to_i
, Time.at.to_f
instead. Open
show_from.nil? ? "" : "<show-from type=\"datetime\">#{Time.at(show_from).xmlschema}</show-from>"
- Read upRead up
- Exclude checks
This cop checks for the use of Time methods without zone.
Built on top of Ruby on Rails style guide (https://github.com/bbatsov/rails-style-guide#time) and the article http://danilenko.org/2012/7/6/rails_timezones/ .
Two styles are supported for this cop. When EnforcedStyle is 'strict' then only use of Time.zone is allowed.
When EnforcedStyle is 'flexible' then it's also allowed to use Time.intimezone.
Example:
# always offense
Time.now
Time.parse('2015-03-02 19:05:37')
# no offense
Time.zone.now
Time.zone.parse('2015-03-02 19:05:37')
# no offense only if style is 'flexible'
Time.current
DateTime.strptime(str, "%Y-%m-%d %H:%M %Z").in_time_zone
Time.at(timestamp).in_time_zone
Use a guard clause instead of wrapping the code inside a conditional expression. Open
unless taglist.nil?
- Read upRead up
- Exclude checks
Use a guard clause instead of wrapping the code inside a conditional expression
Example:
# bad
def test
if something
work
end
end
# good
def test
return unless something
work
end
# also good
def test
work if something
end
# bad
if something
raise 'exception'
else
ok
end
# good
raise 'exception' if something
ok
Prefer single-quoted strings inside interpolations. Open
"<todo>#{props.join("")}</todo>"
- Read upRead up
- Exclude checks
This cop checks that quotes inside the string interpolation match the configured preference.
Example: EnforcedStyle: single_quotes (default)
# bad
result = "Tests #{success ? "PASS" : "FAIL"}"
# good
result = "Tests #{success ? 'PASS' : 'FAIL'}"
Example: EnforcedStyle: double_quotes
# bad
result = "Tests #{success ? 'PASS' : 'FAIL'}"
# good
result = "Tests #{success ? "PASS" : "FAIL"}"
Line is too long. [136/120] Open
"<project><name>#{project[:description]}</name><default-context-id>#{project[:default_context_id]}</default-context-id></project>"
- Exclude checks
Redundant return
detected. Open
return "<context_id>#{context_id}</context_id>"
- Read upRead up
- Exclude checks
This cop checks for redundant return
expressions.
Example:
def test
return something
end
def test
one
two
three
return something
end
It should be extended to handle methods whose body is if/else or a case expression with a default branch.
Use tags.length.positive?
instead of tags.length > 0
. Open
if tags.length > 0
- Read upRead up
- 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
Redundant return
detected. Open
return ""
- Read upRead up
- Exclude checks
This cop checks for redundant return
expressions.
Example:
def test
return something
end
def test
one
two
three
return something
end
It should be extended to handle methods whose body is if/else or a case expression with a default branch.
Redundant return
detected. Open
return "<context><name>#{context_name}</name></context>"
- Read upRead up
- Exclude checks
This cop checks for redundant return
expressions.
Example:
def test
return something
end
def test
one
two
three
return something
end
It should be extended to handle methods whose body is if/else or a case expression with a default branch.
Missing magic comment # frozen_string_literal: true
. Open
require 'active_support/time_with_zone'
- Read upRead up
- Exclude checks
This cop is designed to help upgrade to Ruby 3.0. It will add the
comment # frozen_string_literal: true
to the top of files to
enable frozen string literals. Frozen string literals may be default
in Ruby 3.0. The comment will be added below a shebang and encoding
comment. The frozen string literal comment is only valid in Ruby 2.3+.
Example: EnforcedStyle: when_needed (default)
# The `when_needed` style will add the frozen string literal comment
# to files only when the `TargetRubyVersion` is set to 2.3+.
# bad
module Foo
# ...
end
# good
# frozen_string_literal: true
module Foo
# ...
end
Example: EnforcedStyle: always
# The `always` style will always add the frozen string literal comment
# to a file, regardless of the Ruby version or if `freeze` or `<<` are
# called on a string literal.
# bad
module Bar
# ...
end
# good
# frozen_string_literal: true
module Bar
# ...
end
Example: EnforcedStyle: never
# The `never` will enforce that the frozen string literal comment does
# not exist in a file.
# bad
# frozen_string_literal: true
module Baz
# ...
end
# good
module Baz
# ...
end
Do not use unless
with else
. Rewrite these with the positive case first. Open
unless taglist.nil?
tags = taglist.split(",")
if tags.length > 0
tags = tags.collect { |tag| "<tag><name>#{tag.strip}</name></tag>" unless tag.strip.empty? }.join('')
return "<tags>#{tags}</tags>"
- Read upRead up
- Exclude checks
This cop looks for unless expressions with else clauses.
Example:
# bad
unless foo_bar.nil?
# do something...
else
# do a different thing...
end
# good
if foo_bar.present?
# do something...
else
# do a different thing...
end
Use !empty?
instead of length > 0
. Open
if tags.length > 0
- Read upRead up
- Exclude checks
This cop checks for numeric comparisons that can be replaced by a predicate method, such as receiver.length == 0, receiver.length > 0, receiver.length != 0, receiver.length < 1 and receiver.size == 0 that can be replaced by receiver.empty? and !receiver.empty.
Example:
# bad
[1, 2, 3].length == 0
0 == "foobar".length
array.length < 1
{a: 1, b: 2}.length != 0
string.length > 0
hash.size > 0
# good
[1, 2, 3].empty?
"foobar".empty?
array.empty?
!{a: 1, b: 2}.empty?
!string.empty?
!hash.empty?