It is a bad habit to pass whole parameters to a method as argument?

My concern looks like

Controller Concern:

module User
  extend ActiveSupport::Concern

  def abc(params)
    ...
  end
end

and now I am calling this abc method from my controller

Controller:

  class UserController < AdminController
    ...
    user = abc(params)
    ...
  end

So is it a bad habit to pass the whole parameter from my controller to the abc method?

Upvotes: 2

Views: 183

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230521

Yes, it is a bad practice, because, more often than not, you don't actually need the entirety of the params in that method. You need only a few keys from it, but you pass the whole thing anyway, to make your life easier now.

However, by doing this, you're making your life harder later. When the time comes to revisit/refactor this method, it's not obvious what do you actually use from params. You'll have to analyze all code paths within the method. This is unnecessary work.

Forget the refactoring, even. You'll have to do this every single time you want to call the method.

Compare these two invocations, for example

User.abc(params)

and

User.abc(params[:id], params[:email])

Which of these looks more "manageable"?

Upvotes: 6

Related Questions