Do polymorphism or conditionals promote better design?

前端 未结 12 1805
长情又很酷
长情又很酷 2020-12-02 06:37

I recently stumbled across this entry in the google testing blog about guidelines for writing more testable code. I was in agreement with the author until this point:

相关标签:
12条回答
  • 2020-12-02 07:20

    If you are using switch statements everywhere you run into the possibility that when upgrading you miss one place thats needs an update.

    0 讨论(0)
  • 2020-12-02 07:23

    Switches and polymorphism does the same thing.

    In polymorphism (and in class-based programming in general) you group the functions by their type. When using switches you group the types by function. Decide which view is good for you.

    So if your interface is fixed and you only add new types, polymorphism is your friend. But if you add new functions to your interface you will need to update all implementations.

    In certain cases, you may have a fixed amount of types, and new functions can come, then switches are better. But adding new types makes you update every switch.

    With switches you are duplicating sub-type lists. With polymorphism you are duplicating operation lists. You traded a problem to get a different one. This is the so called expression problem, which is not solved by any programming paradigm I know. The root of the problem is the one-dimensional nature of the text used to represent the code.

    Since pro-polymorphism points are well discussed here, let me provide a pro-switch point.

    OOP has design patterns to avoid common pitfalls. Procedural programming has design patterns too (but no one have wrote it down yet AFAIK, we need another new Gang of N to make a bestseller book of those...). One design pattern could be always include a default case.

    Switches can be done right:

    switch (type)
    {
        case T_FOO: doFoo(); break;
        case T_BAR: doBar(); break;
        default:
            fprintf(stderr, "You, who are reading this, add a new case for %d to the FooBar function ASAP!\n", type);
            assert(0);
    }
    

    This code will point your favorite debugger to the location where you forgot to handle a case. A compiler can force you to implement your interface, but this forces you to test your code thoroughly (at least to see the new case is noticed).

    Of course if a particular switch would be used more than one places, it's cut out into a function (don't repeat yourself).

    If you want to extend these switches just do a grep 'case[ ]*T_BAR' rn . (on Linux) and it will spit out the locations worth looking at. Since you need to look at the code, you will see some context which helps you how to add the new case correctly. When you use polymorphism the call sites are hidden inside the system, and you depend on the correctness of the documentation, if it exists at all.

    Extending switches does not break the OCP too, since you does not alter the existing cases, just add a new case.

    Switches also help the next guy trying to get accustomed to and understand the code:

    • The possible cases are before your eyes. That's a good thing when reading code (less jumping around).
    • But virtual method calls are just like normal method calls. One can never know if a call is virtual or normal (without looking up the class). That's bad.
    • But if the call is virtual, possible cases are not obvious (without finding all derived classes). That's also bad.

    When you provide an interface to a thirdparty, so they can add behavior and user data to a system, then that's a different matter. (They can set callbacks and pointers to user-data, and you give them handles)

    Further debate can be found here: http://c2.com/cgi/wiki?SwitchStatementsSmell

    I'm afraid my "C-hacker's syndrome" and anti-OOPism will eventually burn all my reputation here. But whenever I needed or had to hack or bolt something into a procedural C system, I found it quite easy, the lack of constraints, forced encapsulation and less abstraction layers makes me "just do it". But in a C++/C#/Java system where tens of abstraction layers stacked on the top of each other in the software's lifetime, I need to spend many hours sometimes days to find out how to correctly work around all the constraints and limitations that other programmers built into their system to avoid others "messing with their class".

    0 讨论(0)
  • 2020-12-02 07:25

    Polymorphism is one of the corner stones of OO and certainly is very useful. By dividing concerns over multiple classes you create isolated and testable units. So instead of doing a switch...case where you call methods on several different types or implemenations you create a unified interface, having multiple implementations. When you need to add an implementation, you do not need to modify the clients, as is the case with switch...case. Very important as this helps to avoid regression.

    You can also simplify your client algorithm by dealing with just one type : the interface.

    Very important to me is that polymorphism is best used with a pure interface/implementation pattern ( like the venerable Shape <- Circle etc... ) . You can also have polymorphism in concrete classes with template-methods ( aka hooks ), but its effectiveness decreases as complexity increases.

    Polymorphism is the foundation on which our company's codebase is built, so I consider it very practical.

    0 讨论(0)
  • 2020-12-02 07:26

    Do not fear...

    I guess your problem lies with familiarity, not technology. Familiarize yourself with C++ OOP.

    C++ is an OOP language

    Among its multiple paradigms, it has OOP features and is more than able to support comparison with most pure OO language.

    Don't let the "C part inside C++" make you believe C++ can't deal with other paradigms. C++ can handle a lot of programming paradigms quite graciously. And among them, OOP C++ is the most mature of C++ paradigms after procedural paradigm (i.e. the aforementioned "C part").

    Polymorphism is Ok for production

    There is no "subtle bugs" or "not suitable for production code" thing. There are developers who remain set in their ways, and developers who'll learn how to use tools and use the best tools for each task.

    switch and polymorphism are [almost] similar...

    ... But polymorphism removed most errors.

    The difference is that you must handle the switches manually, whereas polymorphism is more natural, once you get used with inheritance method overriding.

    With switches, you'll have to compare a type variable with different types, and handle the differences. With polymorphism, the variable itself knows how to behave. You only have to organize the variables in logical ways, and override the right methods.

    But in the end, if you forget to handle a case in switch, the compiler won't tell you, whereas you'll be told if you derive from a class without overriding its pure virtual methods. Thus most switch-errors are avoided.

    All in all, the two features are about making choices. But Polymorphism enable you to make more complex and in the same time more natural and thus easier choices.

    Avoid using RTTI to find an object's type

    RTTI is an interesting concept, and can be useful. But most of the time (i.e. 95% of the time), method overriding and inheritance will be more than enough, and most of your code should not even know the exact type of the object handled, but trust it to do the right thing.

    If you use RTTI as a glorified switch, you're missing the point.

    (Disclaimer: I am a great fan of the RTTI concept and of dynamic_casts. But one must use the right tool for the task at hand, and most of the time RTTI is used as a glorified switch, which is wrong)

    Compare dynamic vs. static polymorphism

    If your code does not know the exact type of an object at compile time, then use dynamic polymorphism (i.e. classic inheritance, virtual methods overriding, etc.)

    If your code knows the type at compile time, then perhaps you could use static polymorphism, i.e. the CRTP pattern http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern

    The CRTP will enable you to have code that smells like dynamic polymorphism, but whose every method call will be resolved statically, which is ideal for some very critical code.

    Production code example

    A code similar to this one (from memory) is used on production.

    The easier solution revolved around a the procedure called by message loop (a WinProc in Win32, but I wrote a simplier version, for simplicity's sake). So summarize, it was something like:

    void MyProcedure(int p_iCommand, void *p_vParam)
    {
       // A LOT OF CODE ???
       // each case has a lot of code, with both similarities
       // and differences, and of course, casting p_vParam
       // into something, depending on hoping no one
       // did a mistake, associating the wrong command with
       // the wrong data type in p_vParam
    
       switch(p_iCommand)
       {
          case COMMAND_AAA: { /* A LOT OF CODE (see above) */ } break ;
          case COMMAND_BBB: { /* A LOT OF CODE (see above) */ } break ;
          // etc.
          case COMMAND_XXX: { /* A LOT OF CODE (see above) */ } break ;
          case COMMAND_ZZZ: { /* A LOT OF CODE (see above) */ } break ;
          default: { /* call default procedure */} break ;
       }
    }
    

    Each addition of command added a case.

    The problem is that some commands where similar, and shared partly their implementation.

    So mixing the cases was a risk for evolution.

    I resolved the problem by using the Command pattern, that is, creating a base Command object, with one process() method.

    So I re-wrote the message procedure, minimizing the dangerous code (i.e. playing with void *, etc.) to a minimum, and wrote it to be sure I would never need to touch it again:

    void MyProcedure(int p_iCommand, void *p_vParam)
    {
       switch(p_iCommand)
       {
          // Only one case. Isn't it cool?
          case COMMAND:
             {
               Command * c = static_cast<Command *>(p_vParam) ;
               c->process() ;
             }
             break ;
          default: { /* call default procedure */} break ;
       }
    }
    

    And then, for each possible command, instead of adding code in the procedure, and mixing (or worse, copy/pasting) the code from similar commands, I created a new command, and derived it either from the Command object, or one of its derived objects:

    This led to the hierarchy (represented as a tree):

    [+] Command
     |
     +--[+] CommandServer
     |   |
     |   +--[+] CommandServerInitialize
     |   |
     |   +--[+] CommandServerInsert
     |   |
     |   +--[+] CommandServerUpdate
     |   |
     |   +--[+] CommandServerDelete
     |
     +--[+] CommandAction
     |   |
     |   +--[+] CommandActionStart
     |   |
     |   +--[+] CommandActionPause
     |   |
     |   +--[+] CommandActionEnd
     |
     +--[+] CommandMessage
    

    Now, all I needed to do was to override process for each object.

    Simple, and easy to extend.

    For example, say the CommandAction was supposed to do its process in three phases: "before", "while" and "after". Its code would be something like:

    class CommandAction : public Command
    {
       // etc.
       virtual void process() // overriding Command::process pure virtual method
       {
          this->processBefore() ;
          this->processWhile() ;
          this->processAfter() ;
       }
    
       virtual void processBefore() = 0 ; // To be overriden
       
       virtual void processWhile()
       {
          // Do something common for all CommandAction objects
       }
       
       virtual void processAfter()  = 0 ; // To be overriden
    
    } ;
    

    And, for example, CommandActionStart could be coded as:

    class CommandActionStart : public CommandAction
    {
       // etc.
       virtual void processBefore()
       {
          // Do something common for all CommandActionStart objects
       }
    
       virtual void processAfter()
       {
          // Do something common for all CommandActionStart objects
       }
    } ;
    

    As I said: Easy to understand (if commented properly), and very easy to extend.

    The switch is reduced to its bare minimum (i.e. if-like, because we still needed to delegate Windows commands to Windows default procedure), and no need for RTTI (or worse, in-house RTTI).

    The same code inside a switch would be quite amusing, I guess (if only judging by the amount of "historical" code I saw in our app at work).

    0 讨论(0)
  • 2020-12-02 07:30

    I must re-iterate that finding all switch statments can be a non trivial processes in a mature code base. If you miss any then the application is likely to crash because of an unmatched case statement unless you have default set.

    Also check out "Martin Fowlers" book on "Refactoring"
    Using a switch instead of polymorphism is a code smell.

    0 讨论(0)
  • 2020-12-02 07:30

    It really depends on your style of programming. While this may be correct in Java or C#, I don't agree that automatically deciding to use polymorphism is correct. You can split your code into lots of little functions and perform an array lookup with function pointers (initialized at compile time), for instance. In C++, polymorphism and classes are often overused - probably the biggest design mistake made by people coming from strong OOP languages into C++ is that everything goes into a class - this is not true. A class should only contain the minimal set of things that make it work as a whole. If a subclass or friend is necessary, so be it, but they shouldn't be the norm. Any other operations on the class should be free functions in the same namespace; ADL will allow these functions be used without lookup.

    C++ is not an OOP language, don't make it one. It's as bad as programming C in C++.

    0 讨论(0)
提交回复
热议问题