Do polymorphism or conditionals promote better design?

前端 未结 12 1809
长情又很酷
长情又很酷 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: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++.

提交回复
热议问题