0

Sorry this is a very minor question but the following is part of a contract called LibOrders.sol. The function below calls LibOrders.cancelOrder....normally I think you would just put "cancelOrder" not "LibOrders.cancelOrder" since it is in the same contract. But does it work to say LibOrders.cancelOrder?

Contract LibOrders {

function cancelBuyOrder(address asset, uint16 id) internal {

LibOrders.cancelOrder(asset, id);

}

....

}
        
      

1 Answer 1

1

Yes, it is perfectly valid to call a function from the same contract using the full contract name and function name, i.e. LibOrders.cancelOrder(). This is often done to avoid name collisions, especially in large contracts with many functions.

In your example, it is not strictly necessary to call LibOrders.cancelOrder() since the function is in the same contract. However, it is good practice to do so to make the code more explicit and readable. This is especially important for other developers who may be reading your code and trying to understand what it is doing.

Additionally, calling a function from the same contract using the full contract name and function name can be useful for documentation purposes. For example, if you are using a documentation generator, it will be able to generate more accurate documentation if you use the full contract name and function name.

Overall, there is no harm in calling a function from the same contract using the full contract name and function name. In fact, it is often good practice to do so.

3
  • Can you provide evidence where it says that using the contract name when calling a function is good practice? It could make sense for multiple inheritance contracts, but I'd avoid recommending to do so in general.
    – Ismael
    Commented Sep 19, 2023 at 7:37
  • Right, So for general contracts its not used much. But as you mentioned for inheritence, Its a good practice for nameConflices. Commented Sep 19, 2023 at 7:45
  • Even with multiple inheritance I'd avoid such recommendation. There are just a few corner cases where it makes sense, e.g. when you want to make a call explicit and avoid a conflict with calls to the inherited contracts.
    – Ismael
    Commented Sep 19, 2023 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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