Permalink
8823290 Apr 12, 2016
@troessner @mvz @JuanitoFatas @chastell
57 lines (38 sloc) 1.46 KB

Utility Function

Introduction

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.

Example

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)

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.

Configuration

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:

---
UtilityFunction:
  public_methods_only: true