Best Practice Discussion: Should smart contracts inherit from their corresponding external interfaces?

Over the years I’ve had my smart contracts inherit from their corresponding external interfaces. I’ve used this pattern as that is what OZ has done with the ERC20 and ERC721 token contracts.

Is that the best practice? Is it unnecessary / inefficient in terms of gas?

We received this response from another engineer in the space concerning the topic:

From a style/best practice perspective, it can be nice to define your external interfaces in one place, and implement it in another. It’s especially helpful to others who want to interact with your contract without deploying it themselves, since they can take the much smaller interface file and include that in their own project.

Excited to hear others thoughts. Ty!

1 Like

Hi @dougiebuckets,

My assumption is that if you are implementing your own interface you should inherit from it.
We can use the compiler to ensure that we are implementing the interface as defined in Solidity.

I don’t know about the gas efficiency. My thoughts are that we should write readable/testable/auditable code and let the compiler do its magic to make it as efficient as possible. It would be good to test what the current implementation of the compiler does.

Were they suggesting that interfaces be kept in a separate file, or directory or project (from the implementation contracts)?
I would have thought separate files was enough.
Though I could see an argument for keeping in a separate directory as your API, with the implementation separately.

2 Likes

+1 to this. Also, from a software design standpoint thinking about the interface first and then do the implementation, atleast for me, is the way to go.

This is actually super helpful when implementing an interface

This, from my experience, changes a lot between dev teams.

  • OZ keeps everything under their /contracts directory
  • Others have /contracts & /contracts/interfaces
  • Others have /interfaces & /contracts & /libraries (This is the approach I use for example)

So, IMO, totally up to you.

2 Likes