Table of Contents
Every line of your smart contract costs something.
Some lines cost more than others.
In this part of our gas saving series, we’ll explore how to write smarter Solidity code that keeps your contract lean and efficient.
Here are six simple and practical ways to reduce gas costs while writing Solidity smart contracts.
1. Use payable Only When Needed, But Know It Saves Gas
In Solidity, a function marked payable can actually use slightly less gas than a non-payable one.
Even if you’re not sending ETH, the EVM skips some internal checks when the function is marked payable.
See this example:
function hello() external payable {} // 21,137 gas
function hello2() external {} // 21,161 gas
That tiny difference may not seem like much, but across thousands of calls, it adds up.
- Only use payable when your function is actually meant to accept ETH
2. Use unchecked for Safe Arithmetic When You’re Sure
Since Solidity 0.8.0, all arithmetic operations automatically check for overflows and underflows. While this makes contracts safer, it also uses extra gas. When you’re certain that overflow won’t occur, you can use the unchecked keyword to skip these safety checks.
uint256 public myNumber = 0;
function increment() external {
unchecked {
myNumber++;
}
}
Gas used: 24,347 (much cheaper than using safe math)
Warning: Use unchecked carefully. Only when you’re confident there’s no risk of overflow.
3. Turn On the Solidity Optimizer
The Solidity Optimizer is like a smart helper that cleans up and tightens your compiled bytecode.
It does not change how your contract works, but it removes waste and makes it cheaper to run.
If you’re using tools like Hardhat or Remix, always enable the Optimizer before deploying to mainnet.
4. Use uint256 Instead of Smaller Integers (Most of the Time)
Smaller types like uint8 or uint16 might look more efficient, but they can cost more gas during execution.
That’s because the EVM automatically converts them to uint256 behind the scenes.
So, if you’re not tightly packing them in a struct for storage savings, just use uint256.
Use smaller types only in structs when trying to save storage space.
5. Understand Storage Costs: Read + Write Costs Almost Same as Write
Storage operations are expensive. But here’s something surprising:
Reading a storage variable before writing to it doesn’t cost much more than writing directly.
Example:
- Read + Write = 2,100 (read) + 20,100 (write) = 22,200 gas
- Write only = 22,100 gas
That means if your code needs to read before writing, it’s okay, you are not losing much.
Plan your storage usage wisely. Reuse variables and avoid unnecessary writes.
Reference: https://ethereum.github.io/yellowpaper/paper.pdf
6. Use < Instead of <= in Comparisons
When comparing numbers, use < instead of <=.
Why? Because:
- < needs just one check
- <= takes two checks (a comparison and an extra step to flip the result)
Fewer steps mean lower gas usage.
Example:
for (uint i = 0; i < limit; i++) {
…
}
This small change saves gas on every loop iteration and comparison operation.
Conclusion
Gas optimization is about understanding how the EVM works and making informed decisions about your code structure. Each of these techniques might seem to save small amounts of gas individually, but combined and applied across a large contract, they can result in significant cost savings.
Remember: Always test your optimizations thoroughly. Sometimes gas savings come at the cost of code readability or safety. Strike the right balance for your specific use case.
Key Takeaways:
- Use payable functions when appropriate
- Apply unchecked carefully for safe arithmetic
- Enable the Solidity optimizer
- Prefer uint256 for most variables
- Understand storage access patterns
- Choose efficient comparison operators
By implementing these strategies, you’ll create contracts that not only work well but also cost less to deploy and interact with, making them more accessible to users and more profitable for developers.

Leave a Reply