Sha256: 47fdbaffe2b1b8ef7c83bde52d96627dde002e989098796ceb03a77805e00f6f

Contents?: true

Size: 1.46 KB

Versions: 53

Compression:

Stored size: 1.46 KB

Contents

# 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](Feature-Envy.md)_, please check out the explanation there why _Utility Function_ is something you should care about.

## Example

Given

```Ruby
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](Feature-Envy.md)_ 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](Basic-Smell-Options.md), 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:

```Yaml
---
UtilityFunction:
  public_methods_only: true
```

Version data entries

53 entries across 51 versions & 2 rubygems

Version Path
reek-6.0.3 docs/Utility-Function.md
reek-6.0.2 docs/Utility-Function.md
reek-6.0.1 docs/Utility-Function.md
reek-6.0.0 docs/Utility-Function.md
reek-5.6.0 docs/Utility-Function.md
reek-5.5.0 docs/Utility-Function.md
reek-5.4.1 docs/Utility-Function.md
reek-5.4.0 docs/Utility-Function.md
reek-5.3.2 docs/Utility-Function.md
reek-5.3.1 docs/Utility-Function.md
reek-5.3.0 docs/Utility-Function.md
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/docs/Utility-Function.md
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/docs/Utility-Function.md
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/docs/Utility-Function.md
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/docs/Utility-Function.md
reek-5.2.0 docs/Utility-Function.md
reek-5.1.0 docs/Utility-Function.md
reek-5.0.2 docs/Utility-Function.md
reek-5.0.1 docs/Utility-Function.md
reek-5.0.0 docs/Utility-Function.md