A Utility Function is any instance method that has no dependency on the state of the instance.
Utility Function is heavily related to Feature Envy, please check out the explanation there why Utility Function is something you should care about.
class UtilityFunction def showcase(argument) argument.to_s + argument.to_i end end
Reek would report:
test.rb -- 2 warnings: :UtilityFunction#showcase doesn't depend on instance state (UtilityFunction)
Current Support in Reek
Utility Function will warn about any method that:
- is non-empty
- does not override an inherited method
- calls at least one method on another object
- doesn't use any of self's instance variables
- doesn't use any of self's methods
Differences to Feature Envy
Feature Envy is only triggered if there are some references to self and Utility Function is triggered if there are no references to self.
Reek's Utility Function detector supports the Basic Smell Options, plus:
||Boolean||Disable this smell detector for non-public methods (which means "private" and "protected")|
A sample configuration file would look like this:
--- UtilityFunction: public_methods_only: true