Constants and immutables in Blockchain / Solidity - Time & Space Complexity
When using constants and immutables in blockchain code, it's important to see how they affect the speed of the program.
We want to know if using these fixed values changes how long the code takes as input grows.
Analyze the time complexity of the following code snippet.
contract Example {
uint256 public constant FIXED_VALUE = 100;
uint256 public immutable creationTime;
constructor() {
creationTime = block.timestamp;
}
function multiply(uint256 input) public pure returns (uint256) {
return input * FIXED_VALUE;
}
}
This code defines a constant and an immutable value, then uses the constant in a simple multiplication function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single multiplication operation in the multiply function.
- How many times: Once per function call, no loops or repeated steps inside.
The execution time of multiply does not grow with input size because it only does one multiplication each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 multiplication |
| 100 | 1 multiplication |
| 1000 | 1 multiplication |
Pattern observation: The number of operations per function call remains constant (1 multiplication), regardless of input size.
Time Complexity: O(1)
This means each call to the function takes the same small amount of time, no matter the input size.
[X] Wrong: "Using constants or immutables makes the function slower because they add extra steps."
[OK] Correct: Constants and immutables are fixed values stored efficiently, so accessing them does not slow down the function.
Understanding how constants and immutables affect time helps you write clear and efficient blockchain code, a skill valued in real projects and interviews.
"What if the multiply function used a loop to multiply the input by the constant multiple times? How would the time complexity change?"
Practice
constant and immutable variables in a blockchain smart contract?Solution
Step 1: Understand
constantvariablesconstantvariables are assigned a value at compile time and cannot be changed later.Step 2: Understand
immutablevariablesimmutablevariables are assigned once during contract creation (deployment) and then remain fixed.Final Answer:
constantfixed at compile time,immutableset once at creation -> Option DQuick Check:
constant = compile time, immutable = creation time [OK]
- Thinking immutable can be changed after deployment
- Confusing when constants are assigned
- Assuming both can be modified anytime
MAX_SUPPLY with value 1000 in Solidity?Solution
Step 1: Recall Solidity constant syntax
In Solidity, the type comes first, then the keywordconstant, then the variable name.Step 2: Check each option
uint constant MAX_SUPPLY = 1000; matches the correct syntax:uint constant MAX_SUPPLY = 1000;. Others have wrong order.Final Answer:
uint constant MAX_SUPPLY = 1000; -> Option BQuick Check:
Type + constant + name = correct order [OK]
- Placing constant before type
- Putting constant after variable name
- Missing semicolon at end
contract Example {
uint public immutable creationTime;
constructor() {
creationTime = block.timestamp;
}
}What will
creationTime hold after deployment?Solution
Step 1: Understand
immutableassignmentimmutablevariables can be assigned once during contract creation, here in the constructor.Step 2: Analyze the assignment
creationTime = block.timestamp;sets it to the current block timestamp at deployment.Final Answer:
The timestamp when the contract was deployed -> Option AQuick Check:
Immutable set at deployment time [OK]
- Thinking it holds compile time value
- Assuming default zero value
- Confusing immutable with constant
contract Test {
uint public immutable value;
function setValue(uint _val) public {
value = _val;
}
}Solution
Step 1: Recall rules for
immutablevariablesimmutablevariables can only be assigned once during contract creation, typically in the constructor.Step 2: Check assignment location
Here,valueis assigned inside a public function, which is not allowed.Final Answer:
Cannot assign immutable outside constructor -> Option AQuick Check:
Immutable assigned only once in constructor [OK]
- Trying to assign immutable in regular functions
- Confusing constant and immutable rules
- Thinking public functions can set immutable
contract Token {
// Declare symbol
// Declare deploymentTime
constructor() {
deploymentTime = block.timestamp;
}
}Solution
Step 1: Declare fixed symbol as constant
Since symbol never changes and is known at compile time, declare asconstantwith value assigned inline.Step 2: Declare deploymentTime as immutable
Deployment timestamp is set once at deployment, so declare asimmutableand assign in constructor.Final Answer:
symbol constant with value, deploymentTime immutable assigned in constructor -> Option CQuick Check:
Constant fixed value, immutable set once at deployment [OK]
- Assigning immutable variables inline without constructor
- Declaring constant without value
- Confusing when to use constant vs immutable
Ready to prove it?
Pass the test and this pattern moves unknown
