Mastering Solidity: Constants VS Immutables

Mastering Solidity: Constants VS Immutables

Are you currently learning solidity?
or do you frequently read solidity code?

Then you should read this article to the end.

My goal for this article is to help you understand the differences between Constants and Immutables, and when it's best to use them.


Firstly, let's look at the individual terms and see what they mean, and how they are used:

Constants

A "constant" state is a variable that cannot be changed after the contract is compiled.

contract Example {
    // An example of a constant 
    uint8 public constant speedOfSound = 343;

    // An example of a string variable (not constant)
    string public username = "@CyberDev_D";
}

Immutable

An "immutable" state variable is set during contract creation and it cannot be changed after the contract has been deployed to the blockchain.

contract Example {

    uint256 public immutable timeDeployed;

    constructor() {
        // the timedeployed is set during the contract creation
        timeDeployed = block.timestamp
    }
}

I originally found it a bit confusing to differentiate between a constant and an immutable, it almost seemed like they were the same thing, but here's an illustration that would help solidify the core differences.

The Differences

To illustrate:

Constants

Constants are like the rules that can't be changed after they are set

Imagine you are a builder, and in the community you are assigned to build houses, there is a rule set by the government.

The rule states that "Every room in any house must have two windows".

This rule is set before the building starts, and it doesn't matter the style of house, or the size of it, as long as it has a room it must have two windows. That's the "constant" that has been set.

A constant is set before the building begins

Immutables

When building (Compiling) starts, imagine the first foundation brick you lay, bearing a unique serial number, that is assigned to it only during construction. When construction is complete (deployed), that serial number becomes permanent.
That means it is immutable, it can no longer be changed.

An Immutable is only set during the building process

Using the illustrations above you can note that:

  • Constants are set before the building starts

  • Immutables are set only during the building process

It is important to understand this because it can help you save on gas, if you use them the right way.

For instance :

constant' Keyword: Variables declared with 'constant' are variables that never change after compilation and are hard-coded into the contract. Since these values are constant and do not change, they do not require storage on the blockchain, which would incur additional gas costs. Instead, the value is replaced at compile-time wherever it is referenced in the code. This results in gas savings when interacting with these variables.

'immutable' Keyword: Immutable variables are assigned a value only once, during the contract's construction, and are stored in the contract’s storage. However, unlike regular state variables, accessing immutable variables is more gas-efficient. This is because the Ethereum Virtual Machine (EVM) replaces references to these variables with their assigned values after the contract is deployed, similar to constant variables. As a result, reading from immutable variables costs less gas compared to reading from regular state variables.


Did you learn anything new from this?

If you did,
Please share this article and give me a follow @CyberDev_d