Class: Reek::SmellDetectors::UncommunicativeParameterName

Inherits:
BaseDetector
  • Object
show all
Defined in:
lib/reek/smell_detectors/uncommunicative_parameter_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 UncommunicativeParameterName checks for

  • 1-character names

  • names ending with a number

  • names beginning with an underscore

  • names containing a capital letter (assuming camelCase)

See Uncommunicative-Parameter-Name for details.

Constant Summary collapse

REJECT_KEY =
'reject'
DEFAULT_REJECT_PATTERNS =
[/^.$/, /[0-9]$/, /[A-Z]/, /^_/].freeze
ACCEPT_KEY =
'accept'
DEFAULT_ACCEPT_PATTERNS =
[].freeze

Constants inherited from BaseDetector

BaseDetector::DEFAULT_EXCLUDE_SET, BaseDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from BaseDetector

#config, #context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseDetector

#config_for, configuration_keys, contexts, descendants, #enabled?, #exception?, #expression, inherited, #initialize, #run, #smell_type, smell_type, #smell_warning, #source_line, to_detector, todo_configuration_for, #value

Constructor Details

This class inherits a constructor from Reek::SmellDetectors::BaseDetector

Class Method Details

.default_configObject



30
31
32
33
34
# File 'lib/reek/smell_detectors/uncommunicative_parameter_name.rb', line 30

def self.default_config
  super.merge(
    REJECT_KEY => DEFAULT_REJECT_PATTERNS,
    ACCEPT_KEY => DEFAULT_ACCEPT_PATTERNS)
end

Instance Method Details

#accept_patternsObject (private)



70
71
72
# File 'lib/reek/smell_detectors/uncommunicative_parameter_name.rb', line 70

def accept_patterns
  Array value(ACCEPT_KEY, context)
end

#acceptable_name?(name) ⇒ Boolean (private)

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/reek/smell_detectors/uncommunicative_parameter_name.rb', line 61

def acceptable_name?(name)
  accept_patterns.any? { |accept_pattern| name.match accept_pattern } ||
    reject_patterns.none? { |reject_pattern| name.match reject_pattern }
end

#reject_patternsObject (private)



66
67
68
# File 'lib/reek/smell_detectors/uncommunicative_parameter_name.rb', line 66

def reject_patterns
  Array value(REJECT_KEY, context)
end

#sniffArray<SmellWarning>

Checks the given context for uncommunicative names.

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/reek/smell_detectors/uncommunicative_parameter_name.rb', line 41

def sniff
  params = expression.parameters.select do |param|
    uncommunicative_parameter?(param)
  end

  params.map(&:name).map do |name|
    smell_warning(
      lines: [source_line],
      message: "has the parameter name '#{name}'",
      parameters: { name: name.to_s })
  end
end

#uncommunicative_parameter?(parameter) ⇒ Boolean (private)

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/reek/smell_detectors/uncommunicative_parameter_name.rb', line 56

def uncommunicative_parameter?(parameter)
  !acceptable_name?(parameter.plain_name) &&
    (context.uses_param?(parameter) || !parameter.marked_unused?)
end