Skip to content

Latest commit

 

History

History
57 lines (39 loc) · 1.47 KB

Utility-Function.md

File metadata and controls

57 lines (39 loc) · 1.47 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:

---
detectors:
  UtilityFunction:
    public_methods_only: true