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.