Optional function parameters: Use default arguments (NULL) or overload the function?

前端 未结 12 1548
情书的邮戳
情书的邮戳 2020-12-08 00:44

I have a function that processes a given vector, but may also create such a vector itself if it is not given.

I see two design choices for such a case, where a func

12条回答
  •  情话喂你
    2020-12-08 01:12

    I would not use either approach.

    In this context, the purpose of foo() seems to be to process a vector. That is, foo()'s job is to process the vector.

    But in the second version of foo(), it is implicitly given a second job: to create the vector. The semantics between foo() version 1 and foo() version 2 are not the same.

    Instead of doing this, I would consider having just one foo() function to process a vector, and another function which creates the vector, if you need such a thing.

    For example:

    void foo(int i, const std::vector& optional) {
      // process vector
    }
    
    std::vector* makeVector() {
       return new std::vector;
    }
    

    Obviously these functions are trivial, and if all makeVector() needs to do to get it's job done is literally just call new, then there may be no point in having the makeVector() function. But I'm sure that in your actual situation these functions do much more than what is being shown here, and my code above illustrates a fundamental approach to semantic design: give one function one job to do.

    The design I have above for the foo() function also illustrates another fundamental approach that I personally use in my code when it comes to designing interfaces -- which includes function signatures, classes, etc. That is this: I believe that a good interface is 1) easy and intuitive to use correctly, and 2) difficult or impossible to use incorrectly . In the case of the foo() function we are implictly saying that, with my design, the vector is required to already exist and be 'ready'. By designing foo() to take a reference instead of a pointer, it is both intuitive that the caller must already have a vector, and they are going to have a hard time passing in something that isn't a ready-to-go vector.

提交回复
热议问题