SmellDetector
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
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.
The name of the config field that lists the regexps of smelly names to be reported.
Checks the given context for uncommunicative names.
@return [Array<SmellWarning>]
# File lib/reek/smells/uncommunicative_variable_name.rb, line 59 def examine_context(ctx) @reject_names = value(REJECT_KEY, ctx, DEFAULT_REJECT_SET) @accept_names = value(ACCEPT_KEY, ctx, DEFAULT_ACCEPT_SET) variable_names(ctx.exp).select do |name, lines| is_bad_name?(name, ctx) end.map do |name, lines| SmellWarning.new(SMELL_CLASS, ctx.full_name, lines, "has the variable name '#{name}'", @source, SMELL_SUBCLASS, {VARIABLE_NAME_KEY => name.to_s}) end end
# File lib/reek/smells/uncommunicative_variable_name.rb, line 71 def is_bad_name?(name, ctx) var = name.to_s.gsub(/^[@\*\&]*/, '') return false if @accept_names.include?(var) @reject_names.detect {|patt| patt === var} end
# File lib/reek/smells/uncommunicative_variable_name.rb, line 77 def variable_names(exp) assignment_nodes = exp.each_node(:lasgn, [:class, :module, :defs, :defn]) case exp.first when :class, :module assignment_nodes += exp.each_node(:iasgn, [:class, :module]) end result = Hash.new {|hash, key| hash[key] = []} assignment_nodes.each {|asgn| result[asgn[1]].push(asgn.line) } result end
Generated with the Darkfish Rdoc Generator 2.