P
P
p4s8x2016-09-07 16:03:39
JavaScript
p4s8x, 2016-09-07 16:03:39

Why are long inheritance chains bad?

And so, the situation: We develop client-server applications, constantly work with a large number of entities and plan to use auto-generation of code based on object descriptions.
The standard apache thrift \ protobuf tools did not suit us, so we plan to write our own bike.
The generated code assumes that no manual modifications are made to it, which means that by definition there can be no logic there.
Our generated classes are actually a set of fields (property), given types, with getters / setters and serialization / deserialization methods.
In the standard case, our generated objects have some kind of inheritance chain C_GENERATED <- B_GENERATED A_GENERATED
We can easily further declare our C_CUSTOM <- C_GENERATED and add the logic we need.
But what to do if the logic needs to be declared in an intermediate, generated class, for example, in B_GENERATED (we may have several more classes inherited from B_GENERATED like CA_GENERATED <- B_GENERATED and the logic needs to be added to their parent class)?
There are two solutions:
1) generate an inheritance chain with intermediate custom classes: C_CUSTOM <- C_GEN <-B_CUSTOM <- B_GEN <- A_CUSTOM <- A_GEN
2) generate a symmetrical inheritance chain C_CUSTOM <- B_CUSTOM <- A_CUSTOM, and pass it to custom objects a link to the corresponding generated object. Approximately it might look like this:

class C_CUSTOM extends B_CUSTOM {
  var _c;
  override function setData(C_GEN c) {
    parent::setData(c);
    _c=c
  }
}
class B_CUSTOM extends A_CUSTOM {
  var _b;

  function inc(int i) {
    _b.aaa+=i;
  }
 
  override function setData(B_GEN b) {
    parent::setData(b);
     _b=b;
   }
}
c_custom=new C_CUSTOM();
c_custom.setData(c_gen);
c_custom.inc(10);

In case 1 - with us, the only minus is long inheritance chains (x2 from the original one)
In case 2 - in addition to general cumbersomeness - for example, we need to additionally perform casting of the source object to the parent while moving along the chain
Actually the question is - is it so scary long inheritance ? What are the real downsides to this? Maybe there are other solutions?
Ps Language-dependent "neo-op" solutions like multiple inheritance or its variations in the form of mixin\trait will definitely not/we can't/don't want to use.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Smithson, 2016-09-07
@Smithson

Nothing terrible, in memory these descriptions take up no more than hundreds of bytes per class (not per instance). So don't sweat it.
I would choose option 1, it is somehow more obvious to me.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question