duanjiao8007 2014-10-14 02:50 采纳率: 100%
浏览 68
已采纳

发送单个参数与参数数组

Is there a general good practice convention regarding sending parameters as individual variables, or to send an array of parameters to a function / method?

Eg.

param1, param2, param3 vs array data  

How do you determine which of the two to use, or a combination of both?

  • 写回答

2条回答 默认 最新

  • dongye3917 2014-10-14 02:58
    关注

    When you ask about an array of arguments, I'm assuming you're talking about arguments that are all of the same type (or similar type). It really depends upon the situation and it's a bit of a compromise between convenience for the caller and convenience for the function implementation. That means a lot of it depends upon what you most want to optimize for. There are no hard and fast rules, but you can use this sort of thinking as guidance:

    Use separate arguments if:

    1. The number of arguments is relatively small and usually fixed
    2. The code in the receiving function is much cleaner by having named arguments
    3. The typical way the function call is made is not by building argument lists programatically

    Use an array if:

    1. The number of arguments is relatively large or usually variable (caller probably wants to build an array and pass that)
    2. The receiving function is cleaner by processing a variable list of arguments in a loop (this can be done with the arguments object too, but is sometimes simpler with an actual array)
    3. A common way that the function call is made is from a list of arguments that is built programmatically (more convenient for the caller to just be able to pass the array).
    4. The called function wants to be able to easily pass the list of arguments to some other function call. While this can be done without the array by processing the arguments object, it takes more code to do if the args aren't passed in an array to start with.

    The caller can generally work-around either issue by using .apply() if the function isn't built to take an array, but the caller has arguments in an array.


    FYI (though I don't think this was the main subject of your question), another option is to pass an object with a variable number of properties. The options object is particularly useful when there are a number of different arguments and most or all are optional. The called function can contain default values for all options and the caller can just pass the arguments they want to override the default for. The options object generally isn't the best solution for a variable number of the same type of argument that is better represented in an array or as a list of arguments.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题