Parent

Class/Module Index [+]

Quicksearch

Reek::Smells::ControlCouple

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.

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

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.

One possible solution is to use the Strategy Pattern to pass into the callee what must be done. This is not considered to be control coupling because the callee will do the same thing with the strategy, whatever it happens to be. Sometimes in Ruby the strategy may actually just be a block passed in, and that remains next to where the caller invokes it in the source code.

Public Instance Methods

examine_context(ctx) click to toggle source

Checks whether the given method chooses its execution path by testing the value of one of its parameters.

@return [Array<SmellWarning>]

# File lib/reek/smells/control_couple.rb, line 56
def examine_context(ctx)
  control_parameters(ctx).map do |cond, occurs|
    param = cond.format_ruby
    lines = occurs.map {|exp| exp.line}
    smell = SmellWarning.new(SMELL_CLASS, ctx.full_name, lines,
      "is controlled by argument #{param}",
      @source, SMELL_SUBCLASS,
      {PARAMETER_KEY => param})
    smell
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.