Limit number of parameters per method?

前端 未结 5 919
离开以前
离开以前 2021-02-19 07:57

Assuming the parameters are all the same type, is there a rule of thumb in regards to the number of parameters for a method? Im just wondering where I should draw the line and

5条回答
  •  盖世英雄少女心
    2021-02-19 08:31

    Also, for greater call-site readability, you could use a params parameter list (the varargs mentioned before), so instead of

    void ConstructChildren( Node parent, IEnumerable children)
    ....
    List children = new List {child1, child2, child3};
    ConstructChildren(parent, children);
    

    I would use

    void ConstructChildren( Node parent, params Node[] children)
    ...
    ConstructChildren( parent, child1, child2, child3);
    

    However, the params syntax gets ugly to look at if you use more that 5-6-7 items in the children collection.

提交回复
热议问题