Sha256: f1680e6ca17029059b3dd36e5e6b1329ab08146146e88063674eb416f93aa30d

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

require 'reek/smells/smell_detector'
require 'reek/smell_warning'
require 'reek/sexp_formatter'

module Reek
  module Smells

    #
    # 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 <tt>quoted</tt> 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.
    #
    class ControlCouple < SmellDetector

      def self.contexts      # :nodoc:
        [:if]
      end

      def self.default_config
        super.adopt(EXCLUDE_KEY => ['initialize'])
      end

      def initialize(config = ControlCouple.default_config)
        super
      end

      #
      # Checks whether the given conditional statement relies on a control couple.
      # Remembers any smells found.
      #
      def examine_context(cond)
        return unless cond.tests_a_parameter?
        found(cond, "is controlled by argument #{SexpFormatter.format(cond.if_expr)}")
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
kevinrutherford-reek-1.1.3.3 lib/reek/smells/control_couple.rb
kevinrutherford-reek-1.1.3.4 lib/reek/smells/control_couple.rb
kevinrutherford-reek-1.1.3.5 lib/reek/smells/control_couple.rb
kevinrutherford-reek-1.1.3.6 lib/reek/smells/control_couple.rb
kevinrutherford-reek-1.1.3.7 lib/reek/smells/control_couple.rb
kevinrutherford-reek-1.1.3.8 lib/reek/smells/control_couple.rb
kevinrutherford-reek-1.1.3.9 lib/reek/smells/control_couple.rb