0

Say I have a contract with several functions that apply various particular constants.

circumference = 628318 * radius / 1e5;
dividend = balance * 123 / 1e4;

It would be easier for readers to understand my contract by having these constants defined at the top, and then :

    uint public constant tau = 628318;
    uint public constant dividendYield = 123;
...
    circumference = tau * radius / 1e5;
    dividend = balance * dividendYield / 1e4;

Is there a significant extra memory cost for doing this?

  • No. Constants are replaced by the compiler (or preprocessor, if Solc agrees with this terminology). – goodvibration Oct 28 '19 at 14:39
1

From the official documentation:

The compiler does not reserve a storage slot for these variables, and every occurrence is replaced by the respective constant expression (which might be computed to a single value by the optimizer).

Since neither one of your constant expressions consists of any arithmetic operations (i.e., they are all "literally constant"), you don't even need to worry about the optimizer, and you can rest assure that the compiler will replace them in your code, with no additional gas-cost applied during runtime.

| improve this answer | |

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.