2

In openzeppelin contracts in same cases we have 2 types of functions for the same functionality, one of them preceded by a dash symbol (_). That is the case of transfer(x, y, z)/_transfer(x, y, z) function.

I am aware that when I want to modify the behaviour if I inherit the function, I should use _transfer(x, y, z).

But what's the real difference? Because it happens in lot of functions, and I'm not really sure if I should use transfer(x, y, z) or _transfer(x, y, z).

So then, if I'm not gonna modify its original behaviour, should I use transfer or _transfer?

Thanks in advance

2

The reason for function _transfer is simply code-reuse.

It is called from both functions transfer and transferFrom.

It can also be called from functions mint and burn, if you need to implement them.

And when the exact same functionality is used in several places, it is beneficial to implement it in a single place rather than in several places, because:

  1. It allows you to to fix/change/update the common functionality in a single place
  2. The overall size of your executable (binary) code is subsequently smaller
4
  • So then, if I'm not gonna modify its original behaviour, should I use transfer or _transfer?Thanks
    – AlexAcc
    Dec 19 '20 at 9:37
  • @AlexAcc: What do you mean "use"? From other contracts (as well as from the off-chain), you cannot "use" _transfer since it's internal. So define "use" please. Dec 19 '20 at 9:40
  • Ok sorry, I didn't get the point that _transfer is internal (for only to use inside the contract). So if I need to make a transfer inside of the contract logic I should use _transfer, and if I need to invoke a transfer outside the contract I'd use transfer. Right?
    – AlexAcc
    Dec 19 '20 at 9:58
  • 1
    @AlexAcc: Outside - transfer of course. Inside - depending on your exact purpose, but most likely _transfer. Dec 19 '20 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.