doukuipei9938 2018-05-05 11:56
浏览 100
已采纳

使用数组作为参数的缺点? [关闭]

I always use one array arguments to a function whenever I need more then 3 parameter.

Example: Consider a function call like this.

processSecondStage($stepTitle, $round, $entryId, $formId, $stepType, $stepAfterApproved, $assigneees, $stageToMove);

I always prefer the below one.

processSecondStage(array( 'stepTitle' => $title, 'round' => $round, 'stepAfterApproved' => $stepAfterApproved, 'entryId' => $_entryId, 'assigneees' => (array)$_POST['flow-asignee'], 'stageToMove' => $currentStep, 'formId' => $targetFormId, 'stepType' => 'approval' ));

Advantage (I might be wrong):

a) Can add more number of parameter

b) Readability

c) Order can be changed easily

d) Default parameter handling is easier

Disadvantage :

a) Code length is increase specially in case when we have less then 3-4 arguments.

Can anyone please help me Know more disadvantage of using the array parameter ?

Whenever I change other developer code (like I am going to do now), then I feel that there are some major disadvantage of using array as parameter because if that was not the case then it should have been a standard practice.

Although I have given example of PHP, but I find this in other language also on which I work.

Thank You.

  • 写回答

2条回答 默认 最新

  • doukuizuo1795 2018-05-05 12:41
    关注

    Arrays are useful as a parameter when things you want to pass are closely related and do not make sense separately. A classical example is a Color that is defined by blue, green, red (and maybe gamma). Combining these parameters into an array (or object in javascript) allows you to swap it out easily.

    Always using an array as the single parameter of a function makes you loose out on a parameter list an IDE can give you. For someone reading the code, it is quite a hell to figure out what needs to be passed to a function. It also opens the door to future creep (oh, this function was only making me coffee, but if I add another optional parameter it can also make me tea. Oh, and maybe I can let it make me dinner to. Why not add the functionality to order an attack helicopter too).

    In a regular parameter list you can pass things by reference (function pushElement(array &$sortableArray, $element)). This is not expected in a regular array, if it is even possible.

    The point about default values is kind of a moot point. Normal parameter lists allow type hinting and default values just fine:

    function action(string $action = 'tickle', string $target = 'Polar bear') {
      print "I {$action} a {$target}";
    }
    

    A single parameter as array probably only really shines when passing some kind of static configuration. You keep the configuration of something separate from the actual code using it, making it easier to modify the configuration. At the same time you leverage the fact that you do not have to send 20 parameters in a very specific order.


    In your case the parameter list you give is wildly varied and long, which suggests that your function is doing way too much. Part of it should probably be moved to a constructor and part of it should probably be moved to some kind of Form class.

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

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统