Specific functions vs many Arguments vs context dependent

后端 未结 6 779
生来不讨喜
生来不讨喜 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:13

    I'd avoid your first option because as you say the number of function you end up having to implement (though possibly only as macros) can grow out of control. The count doubles when you decide to add italic support, and doubles again for underline.

    I'd probably avoid the second option as well. Againg consider what happens when you find it necessary to add support for italics or underlines. Now you need to add another parameter to the function, find all of the cases where you called the function and updated those calls. In short, anoying, though once again you could probably simplify the process with appropriate use of macros.

    That leaves the third option. You can actually get some of the benefits of the other alternatives with this using bitflags. For example

    #define WRITE_FORMAT_LEFT   1
    #define WRITE_FORMAT_RIGHT  2
    #define WRITE_FORMAT_CENTER 4
    #define WRITE_FORMAT_BOLD   8
    #define WRITE_FORMAT_ITALIC 16
    ....
    write(char *string, unsigned int format)
    {
      if (format & WRITE_FORMAT_LEFT)
      {
         // write left
      }
    
      ...
    }
    

    EDIT: To answer Greg S.

    I think that the biggest improvement is that it means that if I decide, at this point, to add support for underlined text I it takes two steps

    1. Add #define WRITE_FORMAT_UNDERLINE 32 to the header
    2. Add the support for underlines in write().

    At this point it can call write(..., ... | WRITE_FORMAT_UNLDERINE) where ever I like. More to the point I don't need to modify pre-existing calls to write, which I would have to do if I added a parameter to its signature.

    Another potential benefit is that it allows you do something like the following:

    #define WRITE_ALERT_FORMAT  (WRITE_FORMAT_CENTER | \
                                 WRITE_FORMAT_BOLD |   \
                                 WRITE_FORMAT_ITALIC)
    

提交回复
热议问题