Specific functions vs many Arguments vs context dependent

后端 未结 6 776
生来不讨喜
生来不讨喜 2020-12-20 17:44

An Example

Suppose we have a text to write and could be converted to \"uppercase or lowercase\", and can be printed \"at left, center or right\".

Specific

6条回答
  •  [愿得一人]
    2020-12-20 18:11

    I prefer the argument way.

    Because there's going to be some code that all the different scenarios need to use. Making a function out of each scenario will produce code duplication, which is bad.

    Instead of using an argument for each different case (toUpper, centered etc..), use a struct. If you need to add more cases then you only need to alter the struct:

    typedef struct {
        int toUpper;
        int centered;
        // etc...
    } cases;
    write( char *str , cases c ){//..}
    

提交回复
热议问题