0

I noticed from this answer that there's two ways of separating Contract code with state variables: Inheritance (class Contract is XY {}) or Reference (otherClass = new OtherClass())

I'm wondering what are the pros/cons of using one over the other.

In my mind, I expect initialising a new Contract() to be more expensive since it sounds like it's deploying; yet in that answer, he explains that all the contracts compile down to one in the end, so perhaps it is not more/less expensive.

So why would you choose one over the other?

HJo
  • 193
  • 2
  • 6
  • you split contracts in multiple instances when they execute different functionality. And therefore can be reused by other contracts efficiently – Nulik Jul 19 '21 at 01:25
  • @Nulik thanks, I understand that. My question is what are the pros and cons of each. i.e. why should I use inheritence vs new Contract() – HJo Jul 19 '21 at 14:45
  • this is more like OOP question, I suggest Java forums, or C++ – Nulik Jul 19 '21 at 17:02
  • I see. I wasn't aware that the second design I was referring to was composition pattern, have looked into that now. – HJo Jul 19 '21 at 23:14

1 Answers1

1

Depends on the design of the project. You will see the difference when you work on a project or you go through a project step by step.

I remember there is a limitation if you wanna use inheritance contracts "Linearization of inheritance graph impossible" https://docs.soliditylang.org/en/v0.8.6/contracts.html#multiple-inheritance-and-linearization

The size: it could be exceeded the maximum size for a contract because of duplications, for example.

Check the the access classifiers when use new contract() see the example https://docs.soliditylang.org/en/v0.8.6/contracts.html#visibility-and-getters

anwar
  • 144
  • 8