Sha256: b770acc310001ec5356d3639f2538254ee1558f877b1b8456ed650b885f0e764

Contents?: true

Size: 1.55 KB

Versions: 15

Compression:

Stored size: 1.55 KB

Contents

# Boolean Parameter

## Introduction

`Boolean Parameter` is a special case of [Control Couple](Control-Couple.md), where a method parameter is defaulted
to true or false. A _Boolean Parameter_ effectively permits a method's caller
to decide which execution path to take. This is a case of bad cohesion. You're creating a dependency between methods that is not really necessary, thus increasing coupling.

## Example

Given

```Ruby
class Dummy
  def hit_the_switch(switch = true)
    if switch
      puts 'Hitting the switch'
      # do other things...
    else
      puts 'Not hitting the switch'
      # do other things...
    end
  end
end
```

`reek` would emit the following warning:

```
test.rb -- 3 warnings:
  [1]:Dummy#hit_the_switch has boolean parameter 'switch' (BooleanParameter)
  [2]:Dummy#hit_the_switch is controlled by argument switch (ControlParameter)
```

Note that both smells are reported, `Boolean Parameter` and `Control Parameter`.

## Getting rid of the smell

This is highly dependant on your exact architecture, but looking at the example above what you could do is:

* Move everything in the `if` branch into a separate method
* Move everything in the `else` branch into a separate method
* Get rid of the `hit_the_switch` method alltogether
* Make the decision what method to call in the initial caller of `hit_the_switch`

## Current support in Reek

Reek can only detect a Boolean parameter when it has a default initializer like in the example above.

## Configuration

`Boolean Parameter` supports the [Basic Smell Options](Basic-Smell-Options.md).

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
reek-3.5.0 docs/Boolean-Parameter.md
reek-3.4.1 docs/Boolean-Parameter.md
reek-3.4.0 docs/Boolean-Parameter.md
reek-3.3.1 docs/Boolean-Parameter.md
reek-3.3.0 docs/Boolean-Parameter.md
reek-3.2.1 docs/Boolean-Parameter.md
reek-3.2 docs/Boolean-Parameter.md
reek-3.1 docs/Boolean-Parameter.md
reek-3.0.4 docs/Boolean-Parameter.md
reek-3.0.3 docs/Boolean-Parameter.md
reek-3.0.2 docs/Boolean-Parameter.md
reek-3.0.1 docs/Boolean-Parameter.md
reek-3.0.0 docs/Boolean-Parameter.md
reek-2.2.1 docs/Boolean-Parameter.md
reek-2.2.0 docs/Boolean-Parameter.md