Are type fields pure evil?

后端 未结 9 1332
旧巷少年郎
旧巷少年郎 2021-02-06 16:27

As discusses in The c++ Programming Language 3rd Edition in section 12.2.5, type fields tend to create code that is less versatile, error-prone, less intuitive, and less maintai

相关标签:
9条回答
  • 2021-02-06 17:11

    The best example I can think of (and the one I've run into before), is when your set of types is fixed and the set of functions you want to do (that depend on those types) is fluid. That way, when you add a new function, you modify a single place (adding a single switch) rather than adding a new base virtual function with the real implementation scattered all across the classes in your type hierarchy.

    0 讨论(0)
  • 2021-02-06 17:20

    When you "know" you have a very specific, small, constant set of types, it can be easier to hardcode them like this. Of course, constants aren't and variables don't, so at some point you might have to rewrite the whole thing anyway.

    This is, more or less, the technique used for discriminated unions in several of Alexandrescu's articles.

    For example, if I was implementing a JSON library, I'd know each Value can only be an Object, Array, String, Integer, Boolean, or Null—the spec doesn't allow any others.

    0 讨论(0)
  • 2021-02-06 17:20

    My take is: It depends.

    A parameterized Factory Method design pattern relies on this technique.

    class Creator {
        public:
            virtual Product* Create(ProductId);
    };
    
    Product* Creator::Create (ProductId id) {
            if (id == MINE)  return new MyProduct;
            if (id == YOURS) return new YourProduct;
            // repeat for remaining products...
    
            return 0;
    }
    

    So, is this bad. I don't think so as we do not have any other alternative at this stage. This is a place where it is absolutely necessary as it involves creation of an object. The type of the object is yet to be known.

    The example in OP is however an example which sure needs refactoring. Here we are already dealing with an existing object/type (passed as argument to function).

    As Herb Sutter mentions -

    "Switch off: Avoid switching on the type of an object to customize behavior. Use templates and virtual functions to let types (not their calling code) decide their behavior."

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