Why does Eclipse complain about "Feature envy" smell in my code?

948 Views Asked by At

Eclipse (RedRails) complain about "Feature envy" in the following code:

if input_text =~ /^(---\s*\n.*?\n?)(---.*?)/m
  content_text = input_text[($1.size + $2.size)..-1] # warning in $1

  header = YAML.load($1)

  @content = content_text.strip()
  @title = header["title"]
end

My understanding is that I safe to ignore this warning. But I am wandering why this warning is generated. I cannot understand how I can extract method for $1.size and $1.

1

There are 1 best solutions below

1
On BEST ANSWER

Reek is telling you that, because you are adding two properties of the same class, the calculation should actually belong in String. When adding string lengths this is nonsense of course, but in your case the code can be simplified by using $& (the complete matched string):

input_text[$&.size..-1]