Control Parameter is a special case of Control Couple
Example
A simple example would be the "quoted" parameter in the following method:
defwrite(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".
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:
(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.)
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:
Control Parameter is a special case of Control Couple
Example
A simple example would be the "quoted" parameter in the following method:
defwrite(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".
A Long Parameter List occurs when a method has a lot of parameters.
Example
Given
classDummy
deflong_list(foo,bar,baz,fling,flung)
puts foo,bar,baz,fling,flung
end
end
Reek would report the following warning:
test.rb -- 1 warning:
[2]:Dummy#long_list has 5 parameters (LongParameterList)
A common solution to this problem would be the introduction of parameter objects.
Too Many Instance Variables is a special case of LargeClass.
Example
Given this configuration
TooManyInstanceVariables:
max_instance_variables:3
and this code:
classTooManyInstanceVariables
definitialize
@arg_1=:dummy
@arg_2=:dummy
@arg_3=:dummy
@arg_4=:dummy
end
end
Reek would emit the following warning:
test.rb -- 5 warnings:
[1]:TooManyInstanceVariables has at least 4 instance variables (TooManyInstanceVariables)
Control Parameter is a special case of Control Couple
Example
A simple example would be the "quoted" parameter in the following method:
defwrite(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".
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:
classWarehouse
defsale_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.
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:
defdouble_thing()
@other.thing +@other.thing
end
One quick approach to silence Reek would be to refactor the code thus:
defdouble_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:
classOther
defdouble_thing()
thing + thing
end
end
The approach you take will depend on balancing other factors in your code.
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:
defdouble_thing()
@other.thing +@other.thing
end
One quick approach to silence Reek would be to refactor the code thus:
defdouble_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:
classOther
defdouble_thing()
thing + thing
end
end
The approach you take will depend on balancing other factors in your code.
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:
defdouble_thing()
@other.thing +@other.thing
end
One quick approach to silence Reek would be to refactor the code thus:
defdouble_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:
classOther
defdouble_thing()
thing + thing
end
end
The approach you take will depend on balancing other factors in your code.
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 35.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 35.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
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:
defdouble_thing()
@other.thing +@other.thing
end
One quick approach to silence Reek would be to refactor the code thus:
defdouble_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:
classOther
defdouble_thing()
thing + thing
end
end
The approach you take will depend on balancing other factors in your code.
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:
defdouble_thing()
@other.thing +@other.thing
end
One quick approach to silence Reek would be to refactor the code thus:
defdouble_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:
classOther
defdouble_thing()
thing + thing
end
end
The approach you take will depend on balancing other factors in your code.