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 ending with a number

Constant Summary collapse

REJECT_KEY =

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

'reject'
DEFAULT_REJECT_SET =
[/^.$/, /[0-9]$/]
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'
DEFAULT_ACCEPT_SET =
['Inline::C']

Constants inherited from SmellDetector

SmellDetector::DEFAULT_EXCLUDE_SET, SmellDetector::EXCLUDE_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

class_name, #configure, #configure_with, #copy, create, #enabled?, #examine, #exception?, #found, #has_smell?, listen, #listen_to, #num_smells, #report_on, #smell_name, #smelly?, #supersede_with, #value

Constructor Details

#initialize(config = UncommunicativeName.default_config) ⇒ UncommunicativeName

Returns a new instance of UncommunicativeName.



46
47
48
# File 'lib/reek/smells/uncommunicative_name.rb', line 46

def initialize(config = UncommunicativeName.default_config)
  super(config)
end

Class Method Details

.contextsObject

:nodoc:



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

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

.default_configObject



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

def self.default_config
  super.adopt(
    REJECT_KEY => DEFAULT_REJECT_SET,
    ACCEPT_KEY => DEFAULT_ACCEPT_SET
  )
end

Instance Method Details

#consider_name(context) ⇒ Object

:nodoc:



66
67
68
69
70
71
# File 'lib/reek/smells/uncommunicative_name.rb', line 66

def consider_name(context)  # :nodoc:
  name = context.name
  return false if value(ACCEPT_KEY, context, DEFAULT_ACCEPT_SET).include?(context.to_s)  # TODO: fq_name() ?
  return false unless is_bad_name?(name, context)
  found(context, "has the name '#{name}'")
end

#consider_variables(context) ⇒ Object

:nodoc:



59
60
61
62
63
64
# File 'lib/reek/smells/uncommunicative_name.rb', line 59

def consider_variables(context) # :nodoc:
  context.variable_names.each do |name|
    next unless is_bad_name?(name, context)
    found(context, "has the variable name '#{name}'")
  end
end

#examine_context(context) ⇒ Object

Checks the given context for uncommunicative names. Remembers any smells found.



54
55
56
57
# File 'lib/reek/smells/uncommunicative_name.rb', line 54

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

#is_bad_name?(name, context) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/reek/smells/uncommunicative_name.rb', line 73

def is_bad_name?(name, context)  # :nodoc:
  var = name.effective_name
  return false if var == '*' or value(ACCEPT_KEY, context, DEFAULT_ACCEPT_SET).include?(var)
  value(REJECT_KEY, context, DEFAULT_REJECT_SET).detect {|patt| patt === var}
end