Elegant way to pass multiple arguments to a function

前端 未结 6 1606
轮回少年
轮回少年 2020-12-13 05:52

I\'ve got a function which looks like this:

bool generate_script (bool net, bool tv, bool phone,
                        std::string clientsID,
                      


        
6条回答
  •  情书的邮戳
    2020-12-13 06:51

    Ignoring the possibility or desirability of changing the function or program in some way as to reduce the number of parameters...

    I have seen coding standards that specify how long parameter lists should be formatted, for cases where refactoring is not possible. One such example is using double indentations and one parameter per line (Not for all functions - only for those that have multiple-lines of parameters).

    E.g.

    bool generate_script (
            bool net,
            bool tv,
            bool phone,
            std::string clientsID,
            std::string password,
            int index,
            std::string number,
            std::string Iport,
            std::string sernoID,
            std::string VoiP_number,
            std::string  VoiP_pass,
            std::string target,
            int slot,
            int port,
            int onu,
            int extra,
            std::string IP,
            std::string MAC);
    

    The point here is to create a consistent layout and look for all functions with a large number of parameters.

提交回复
热议问题