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.
Given
class UtilityFunction
def showcase(argument)
argument.to_s + argument.to_i
end
end
Reek would report:
test.rb -- 2 warnings:
[2]:UtilityFunction#showcase doesn't depend on instance state (UtilityFunction)
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
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:
Option | Value | Effect |
---|---|---|
public_methods_only |
Boolean | Disable this smell detector for non-public methods (which means "private" and "protected") |
A sample configuration file would look like this:
---
detectors:
UtilityFunction:
public_methods_only: true