Method sample_named
has 50 lines of code (exceeds 25 allowed). Consider refactoring. Open
def sample_named(name, study)
{
'sample_tube_attributes' => {
'two_dimensional_barcode' => ''
},
WorkingSetup::StandardSeeder has at least 16 methods Open
class StandardSeeder
- Read upRead up
- Exclude checks
Too Many Methods
is a special case of LargeClass
.
Example
Given this configuration
TooManyMethods:
max_methods: 3
and this code:
class TooManyMethods
def one; end
def two; end
def three; end
def four; end
end
Reek would emit the following warning:
test.rb -- 1 warning:
[1]:TooManyMethods has at least 4 methods (TooManyMethods)
WorkingSetup::StandardSeeder#plates_of_purpose has approx 6 statements Open
def plates_of_purpose(name, number) # rubocop:todo Metrics/AbcSize
- Read upRead up
- Exclude checks
A method with Too Many Statements
is any method that has a large number of lines.
Too Many Statements
warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements
counts +1 for every simple statement in a method and +1 for every statement within a control structure (if
, else
, case
, when
, for
, while
, until
, begin
, rescue
) but it doesn't count the control structure itself.
So the following method would score +6 in Reek's statement-counting algorithm:
def parse(arg, argv, &error)
if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
return nil, block, nil # +1
end
opt = (val = parse_arg(val, &error))[1] # +2
val = conv_arg(*val) # +3
if opt and !arg
argv.shift # +4
else
val[0] = nil # +5
end
val # +6
end
(You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)
WorkingSetup::StandardSeeder#plates_of_purpose refers to 'plate' more than self (maybe move it to another class?) Open
plate.wells.each do |w|
w.aliquots.create!(
sample:
Sample.create!(name: "sample_#{plate.human_barcode}_#{w.map.description}", studies: [study, study_b])
)
- 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.
WorkingSetup::StandardSeeder#tag_plates has approx 6 statements Open
def tag_plates( # rubocop:todo Metrics/MethodLength
- Read upRead up
- Exclude checks
A method with Too Many Statements
is any method that has a large number of lines.
Too Many Statements
warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements
counts +1 for every simple statement in a method and +1 for every statement within a control structure (if
, else
, case
, when
, for
, while
, until
, begin
, rescue
) but it doesn't count the control structure itself.
So the following method would score +6 in Reek's statement-counting algorithm:
def parse(arg, argv, &error)
if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
return nil, block, nil # +1
end
opt = (val = parse_arg(val, &error))[1] # +2
val = conv_arg(*val) # +3
if opt and !arg
argv.shift # +4
else
val[0] = nil # +5
end
val # +6
end
(You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)
Complex method WorkingSetup::StandardSeeder#plates_of_purpose (25.5) Open
def plates_of_purpose(name, number) # rubocop:todo Metrics/AbcSize
purpose = Purpose.find_by!(name: name)
number.times do
purpose.create!.tap do |plate|
plate.wells.each do |w|
- 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
WorkingSetup::StandardSeeder tests 'existing' at least 3 times Open
return existing if existing
User
.create!(login: 'admin', password: 'admin', swipecard_code: 'abcdef', barcode: 'ID99A')
.tap(&:grant_administrator)
- Read upRead up
- Exclude checks
Repeated Conditional
is a special case of Simulated Polymorphism
. Basically it means you are checking the same value throughout a single class and take decisions based on this.
Example
Given
class RepeatedConditionals
attr_accessor :switch
def repeat_1
puts "Repeat 1!" if switch
end
def repeat_2
puts "Repeat 2!" if switch
end
def repeat_3
puts "Repeat 3!" if switch
end
end
Reek would emit the following warning:
test.rb -- 4 warnings:
[5, 9, 13]:RepeatedConditionals tests switch at least 3 times (RepeatedConditional)
If you get this warning then you are probably not using the right abstraction or even more probable, missing an additional abstraction.
WorkingSetup::StandardSeeder#plates_of_purpose calls 'plate.human_barcode' 2 times Open
Sample.create!(name: "sample_#{plate.human_barcode}_#{w.map.description}", studies: [study, study_b])
)
end
puts "#{name}: #{plate.ean13_barcode}-#{plate.human_barcode}"
- 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.
WorkingSetup::StandardSeeder#tag_plates calls 'Time.current' 2 times Open
lot_number: Time.current.to_i.to_s,
template: TagLayoutTemplate.find_by!(name: template),
user: user,
received_at: Time.current
- 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.
Complex method WorkingSetup::StandardSeeder#tag_plates (22.6) Open
def tag_plates( # rubocop:todo Metrics/MethodLength
lot_type: 'IDT Tags',
template: 'Sanger_168tags - 10 mer tags in columns ignoring pools (first oligo: ATCACGTT)',
size: 30
)
- 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
WorkingSetup::StandardSeeder#create_or_find_user doesn't depend on instance state (maybe move it to another class?) Open
def create_or_find_user
- Read upRead up
- Exclude checks
A Utility Function is any instance method that has no dependency on the state of the instance.
WorkingSetup::StandardSeeder#supplier doesn't depend on instance state (maybe move it to another class?) Open
def supplier
- Read upRead up
- Exclude checks
A Utility Function is any instance method that has no dependency on the state of the instance.
WorkingSetup::StandardSeeder#create_project doesn't depend on instance state (maybe move it to another class?) Open
def create_project(name)
- Read upRead up
- Exclude checks
A Utility Function is any instance method that has no dependency on the state of the instance.
WorkingSetup::StandardSeeder#plates_of_purpose has the variable name 'w' Open
plate.wells.each do |w|
- Read upRead up
- Exclude checks
An Uncommunicative Variable Name
is a variable name that doesn't communicate its intent well enough.
Poor names make it hard for the reader to build a mental picture of what's going on in the code. They can also be mis-interpreted; and they hurt the flow of reading, because the reader must slow down to interpret the names.
WorkingSetup::StandardSeeder#seed has the variable name 's' Open
Sample.find_each { |s| study_b.samples << s }
- Read upRead up
- Exclude checks
An Uncommunicative Variable Name
is a variable name that doesn't communicate its intent well enough.
Poor names make it hard for the reader to build a mental picture of what's going on in the code. They can also be mis-interpreted; and they hurt the flow of reading, because the reader must slow down to interpret the names.