2

I haven't found a definitive answer to this question.

It seems to have be raised as an open issue (#12675) in Solidity's repository:

Immutable style guide #12675

Page

https://docs.soliditylang.org/en/v0.8.10/style-guide.html

Abstract

There is nothing that references the immutable variable style guides.

Should it follow the syntax of CONSTANTS_WITH_UNDERSCORES or camelCase?

But the PR (#13955) that tried to document didn't find a clear answer and it was closed.

I'm not sure if making a choice about it now is a good idea. I'd be inclined to just mention both possibilities and wait until someone complains and backs it up with good arguments to choose one over the other. @cameel

Probably best to just search github for the most common practice and just document that. It'd make sense to align it with constants or with state variables or to make it deliberately distinct from either of them. @ekpyron

By v0.8.19, Solidity style guide hasn't added any immutable reference.

Searching the OpenZeppelin contracts, I have found a preference over camelCase. For example:

abstract contract ERC20Wrapper is ERC20 {
    IERC20 public immutable underlying;

    constructor(IERC20 underlyingToken) {
        underlying = underlyingToken;
    }

    ...
}

But there are also examples of CONSTANT_CASE:

abstract contract EIP712 {
    /* solhint-disable var-name-mixedcase */
    // Cache the domain separator as an immutable value, but also store the chain id that it corresponds to, in order to
    // invalidate the cached domain separator if the chain id changes.
    bytes32 private immutable _CACHED_DOMAIN_SEPARATOR;
    uint256 private immutable _CACHED_CHAIN_ID;
    address private immutable _CACHED_THIS;

    bytes32 private immutable _HASHED_NAME;
    bytes32 private immutable _HASHED_VERSION;
    bytes32 private immutable _TYPE_HASH;

    ...
}

So, being there cases for both, which naming convention should we follow? CONSTANT_CASE or camelCase?

3
  • CONSTANT_CASE is the one we all use Commented Mar 21, 2023 at 5:29
  • @SolidityLearner who do you mean by all? I think I'm seeing a preference for camelCase rather than for CONSTANT_CASE by the community.
    – tinom9
    Commented Mar 21, 2023 at 11:21
  • 1
    Maybe because it's written in camelCase inside the docs. I have seen many people using CONSTANT_CASE. I prefer it too, to keep them different from the normal variables. Constant and immutable have a very slight difference that's why I keep the naming convention the same for them. Commented Mar 21, 2023 at 12:53

2 Answers 2

1

Another option, that I prefer to use, would be to prefix it with i_, like the Chainlink GitHub suggests.

Example:

uint256 private immutable i_myImmutVar;
uint256 private constant MY_CONST_VAR;
0

For immutable variables in Solidity, the convention is to use CAPS_CASE or UPPER_CASE_WITH_UNDERSCORES, similar to constants in other programming languages. This helps to differentiate them from regular variables and makes it clear that they are immutable.

However, there is no hard and fast rule for naming conventions in Solidity. Some developers might prefer to use camelCase for immutable variables, especially if they are only used within a single function or contract.

Ultimately, the most important thing is to be consistent within your own codebase and follow any conventions set by your team or project.

2
  • What makes you say the convention is to use CONSTANT_CASE?
    – tinom9
    Commented Mar 23, 2023 at 8:43
  • 1
    Convention is how something is usually done, especially within a particular area or activity. According to my answer, no hard-coded rule regarding naming immutable variables exists. You and your team choose it as your naming convention. I prefer UPPER_CASE_WITH_UNDERSCORES; others may decide otherwise. Either way is acceptable. Just to confirm your own research, there is no definitive answer is answer. :)
    – PSS
    Commented Mar 24, 2023 at 2:08

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.