开发者

Accepting arguments individually vs an array

开发者 https://www.devze.com 2023-03-07 05:18 出处:网络
What are the pros and cons of each? For example: my_func(a, b, c, d, e) vs my_func(params). Ge开发者_如何学Pythonnerally, I like the former because intellisense can detect it for people that use IDE

What are the pros and cons of each?

For example: my_func(a, b, c, d, e) vs my_func(params).

Ge开发者_如何学Pythonnerally, I like the former because intellisense can detect it for people that use IDE's. Whereas using the second method requires the developer to lookup documentation. However, I've noticed that Facebook's JS SDK heavily uses the latter.


One clear advantage to accepting an Array of arguments is that you are free to add (or remove!) new arguments without impacting existing implementations as the method signature will not change.

Another possible reason for choosing to pass an arguments Array is so you can mimic named parameters which can make methods more flexible and avoid having to have a method signature that accepts a large number of arguments.


It depends on what you are passing, if its a group of related parameters, then an array is useful. Many jQuery plugins gather a group of options as an associative array, and then other methods. I commonly do this when writing plugins:

var options = { option1: 'stuff', option2: 'etc' };
$('#id').myPlugin(options, 'disengage');

Where the second parameter is the action you want the plugin to perform.


Indeed, that is a pro to the multi-parameter method.

As people have mentioned here, the advantage to a js object (an array here isn't as nice IMO) is that the function signature becomes flexible. Not that javascript enforces this like other languages, but still it helps to be able to not have to change the function signature every time you want to change the parameters.

Facebook, to use the example you brought, change their SDK's and other protocols every 10 minutes (I hated developing with Facebook's stuff, their documentation is appalling and they have a sadistic tendency to slip the carpet from under your feet by bringing out new versions of everything without backward-compatibility. Although this is not the place for this rant). So for them, this would be helpful.

One distinction that I find useful in some scenarios:

Often when a function performs a certain action, there are parameters that define on what values the function should perform its task. For instance compare(3,4) will perform the relevant arithmetic on the arguments 3 and 4.

Another group of parameters are those that define how the function should behave. For example compare(3,4,'strict'). Here, I'm not comparing 'strict' to anything, rather that parameter tells the function that I want to perform a specific type of comparison.

In this example it seems unlikely that much will change so the topic is mostly irrelevant, although on larger more complex functions this isn't the case. I often find that the most "stable" parameters are the values, around which the definition of the function is usually based. Whereas behavioural parameters change more often, and more often don't exist.

In addition to making the method signature more "stable", it makes code more readable because on each function call you can usually see what the operands of the function will be, as opposed to a single object, which is more difficult to understand and debug.

0

精彩评论

暂无评论...
验证码 换一张
取 消