In OOP languages, composition over inheritance is a well-known best practice. Solidity is an OOP language too but there is also the gas efficiency issue.
Question is, in Solidity, how do composition and inheritance compare to each other considering respective gas costs?
P.S. Also asked in Ethereum.SE: https://ethereum.stackexchange.com/questions/59994/composition-over-inheritance-gas-efficiency
In a 1-1 comparison, Composition is more expensive to deploy and execute. That said, if you need to deploy many instances, you could use the Library pattern and use composition in that way.
Libraries can lower deployment costs if you're going to be deploying many copies of the same code. If you imagine deploying 100 copies of the same code, you may only have to deploy one Library, and then the more code you can push into the Library, the cheaper each dependent contract deployment will be. But calling that library increases execution costs.
Another thing to consider with libraries: there are significant security concerns to shared libraries. The Parity hack which locked up funds was due to usage of composition.
Inheritance is a much simpler model and will always be cheaper to execute.
But, you will have to make the call as to what fits your use case. Will there be many versions of this contract? If so, will each instance be executed often enough to make up for deployment costs?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With