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
For your code using the params hash, you should recognize that
params[:some]is the same asparams.[](: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: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.