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

时间:2017-12-18 07:43:24

标签: ruby-on-rails ruby

My concern looks like

Controller Concern:

(Get-WmiObject -Class Win32_IP4RouteTable -Filter "Destination = '0.0.0.0' AND Mask = '0.0.0.0'" |    
        Sort-Object metric1 | Select-Object -First 1).nexthop

and now I am calling this module User extend ActiveSupport::Concern def abc(params) ... end end method from my controller

Controller:

abc

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

1 个答案:

答案 0 :(得分:6)

Yes, it is a bad practice, because, more often than not, you don't actually need the entirety of the <script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/data.js"></script> <script src="https://code.highcharts.com/modules/drilldown.js"></script> <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div> 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

params

and

User.abc(params)

Which of these looks more "manageable"?