3

How to rectify DuplicateMethodCall from reek on the following calls

def to_str
    foo.blank? ? 'Value' : foo
end 

How should I handle params[:some] should I declare it separately.

if params[:some] == 'Action1'

elsif params[:some] == 'Action2'

elsif params[:some] == 'Action3'

elsif params[:some] == 'Action4'

end
Sam
  • 8,387
  • 19
  • 62
  • 97

1 Answers1

1

For your code using the params hash, you should recognize that params[:some] is the same as params.[](:some). Because of this, Reek is assuming that you are repeatedly calling the same method (:[]) on an object (params).

In the case of a params hash, this warning can seem a bit silly, since hash key lookup is very fast. However, to correct this, you can assign the params[:some] value to a local variable:

some = params[:some]

if some == 'Action1'

elsif some == 'Action2'

elsif some == 'Action3'

elsif some == 'Action4'

Even though the performance gains are minimal, this code is (arguably) more readable and easier to maintain.

Bear in mind that while simply fixing reported problems is helpful, the real power of code metrics is that they focus you on problem areas in your code, giving you the opportunity to rethink your approach. Eliminating or refactoring a code smell is better than just patching it to quiet down a code metric complaint.

Eric Budd
  • 511
  • 1
  • 4
  • 12