public
Description: Code smell detector for Ruby
Home | Edit | New

Control Couple

Control coupling occurs when a method or block checks the value of a parameter in order to decide which execution path to take. The offending parameter is often called a “Control Couple”.

Control Coupling is a kind of duplication, because the calling method already knows which path should be taken.

Control Coupling reduces the code’s flexibility by creating a dependency between the caller and callee: any change to the possible values of the controlling parameter must be reflected on both sides of the call. A Control Couple also reveals a loss of simplicity: the called method probably has more than one responsibility, because it includes at least two different code paths.

Current Support in Reek

Currently Reek warns about control coupling when:

  • a method parameter or block parameter is the tested value in a conditional statement (as in the example below); or
  • (since release 1.2.4) a method parameter is defaulted to true or false.

Configuration

Control Couple supports only the Basic Smell Options. The file config/defaults.reek (shipped with the Reek gem) lists the default configuration settings for this smell.

Example

A simple example would be the quoted parameter in the following method:

def write(quoted)
  if quoted
    write_quoted(@value)
  else
    puts @value
  end
end

To stop Reek reporting this smell, you might create a configuration file containing this:
---
ControlCouple: 
  exclude:
  - write

or this:
---
ControlCouple: 
  exclude:
  - !ruby/regexp /write/

See Also

Last edited by kevinrutherford, Tue Nov 17 03:56:33 -0800 2009
Home | Edit | New
Versions: