Class: Reek::Smells::UncommunicativeName

Inherits:
SmellDetector show all
Defined in:
lib/reek/smells/uncommunicative_name.rb

Overview

An Uncommunicative Name is a name that doesn’t communicate its intent well enough.

Poor names make it hard for the reader to build a mental picture of what’s going on in the code. They can also be mis-interpreted; and they hurt the flow of reading, because the reader must slow down to interpret the names.

Currently UncommunicativeName checks for

  • 1-character names

  • names consisting of a single character followed by a number

Constant Summary collapse

REJECT_KEY =

The name of the config field that lists the regexps of smelly names to be rejected.

'reject'
ACCEPT_KEY =

The name of the config field that lists the specific names that are to be treated as exceptions; these names will not be reported as uncommunicative.

'accept'
VERIFIER_EXTENSION_KEY =

The name of the config field that list the verifier extention ruby scripts. This script contains a class which is used for verifying the variable with their associated context.

'verifierExtention'

Constants inherited from SmellDetector

SmellDetector::ENABLED_KEY, SmellDetector::EXCLUDE_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

class_name, #examine, #exception?, listen, #smell_name

Constructor Details

#initialize(config = UncommunicativeName.default_config) ⇒ UncommunicativeName

Returns a new instance of UncommunicativeName.



47
48
49
50
51
52
# File 'lib/reek/smells/uncommunicative_name.rb', line 47

def initialize(config = UncommunicativeName.default_config)
  super
  @reject = config[REJECT_KEY]
  @accept = config[ACCEPT_KEY]
  @verifier_extensions = config[VERIFIER_EXTENSION_KEY]
end

Class Method Details

.contextsObject

:nodoc:



43
44
45
# File 'lib/reek/smells/uncommunicative_name.rb', line 43

def self.contexts      # :nodoc:
  [:module, :class, :defn, :defs, :iter]
end

.default_configObject



36
37
38
39
40
41
# File 'lib/reek/smells/uncommunicative_name.rb', line 36

def self.default_config
  super.adopt(
    REJECT_KEY => [/^.[0-9]*$/],
    ACCEPT_KEY => ['Inline::C']
  )
end

Instance Method Details

#consider_name(context, report) ⇒ Object

:nodoc:



82
83
84
85
86
87
88
# File 'lib/reek/smells/uncommunicative_name.rb', line 82

def consider_name(context, report)  # :nodoc:
  name = context.name
  return false if @accept.include?(context.to_s)  # TODO: fq_name() ?
  return false unless is_bad_name?(name)
  report << SmellWarning.new(self, context,
                "has the name '#{name}'")
end

#consider_variables(context, report) ⇒ Object

:nodoc:



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/reek/smells/uncommunicative_name.rb', line 63

def consider_variables(context, report) # :nodoc:
  context.variable_names.each do |name|

    report << SmellWarning.new(self, context,
              "has the variable name '#{name}'") if is_bad_name?(name)

    accepted, message = verifier_extension_accepted?(context, name)
    if !accepted
      report << SmellWarning.new(self, context, message)
    end
  end
end

#examine_context(context, report) ⇒ Object

Checks the given context for uncommunicative names. Any smells found are added to the report.



58
59
60
61
# File 'lib/reek/smells/uncommunicative_name.rb', line 58

def examine_context(context, report)
  consider_name(context, report)
  consider_variables(context, report)
end

#is_bad_name?(name) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/reek/smells/uncommunicative_name.rb', line 90

def is_bad_name?(name)  # :nodoc:
  var = name.effective_name
  return false if var == '*' or @accept.include?(var)
  @reject.detect {|patt| patt === var}
end

#verifier_extension_accepted?(p_context, p_name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/reek/smells/uncommunicative_name.rb', line 76

def verifier_extension_accepted?(p_context, p_name)
  return true, nil if !@verifier_extensions
  accepted, message = VerifierExtensionManager::accepted?(@verifier_extensions, p_context, p_name)
  return accepted, message
end