Why Every Byte Counts on the BlockchainGas isn’t just some abstract developer thing — it’s actual money. Every transaction you run, every function call you make on Ethereum (or any EVM-based chain) comes with a price. That price? Gas.
Now sure, if you’re new to Solidity, it might not seem like a huge deal at first. But trust me — once your contracts start getting used thousands of times, every little inefficiency turns into real costs. Even something as small as reading a variable too many times can hit your users in the wallet.
This post walks through five simple-but-powerful ways to save gas. And no, they’re not magic tricks — just smart coding habits that pay off over time.
The Foundation: Understanding Gas MechanicsAlright, quick refresher before we jump in. If you want to optimize gas, you need to know where it’s going.
Here’s the breakdown:
The EVM likes to store things in 32-byte slots. That’s 256 bits per slot. So, if you’re declaring a bunch of small variables one after another — like bool, uint8, etc.—and spacing them out in the contract, Solidity might just throw each one into its own full slot. Wasteful, right?
The Fix: Pack smaller variables together so they sit side-by-side in one slot. You’ll instantly save gas, especially on deployment.
// Inefficient - Uses 3 separate storage slots.This one’s pretty straightforward, and yet a lot of folks miss it.
Here’s the thing: reading from storage isn’t free. If you’re doing it multiple times inside a single function, guess what? You’re paying for each read — even if it’s the exact same value.
The Fix: Pull the value into a local variable once. Do your work there. Then write it back to storage (if needed) at the end.
// Inefficient - Reads 'userPoints[msg.sender]' from storage three times.Ever noticed how Solidity makes you choose between memory and calldata for function parameters like arrays or strings? A lot of devs just default to memory because it feels familiar.
But here’s the catch: when you use memory, Solidity makes a full copy of the input. And yep—copying data costs gas.
The Fix: If you’re not modifying the input, use calldata. It avoids the copy step and keeps things lean.
// Inefficient - Uses 'memory'. The array is copied from calldata into memory.This one’s sneaky. Let’s say you’ve got a require statement with two conditions. One is quick and easy—like checking a local variable. The other is a heavy function call (maybe signature verification or an external lookup).
If you put the expensive one first, it gets executed even if the cheap one fails.
The Fix: Put the least expensive (or most likely to fail) condition first.
// Inefficient - Expensive check runs even if the cheaper one would fail.Got a value that never changes? Like a max supply or owner address?
If you declare it as a regular state variable, Solidity puts it in storage. And every time you access it, you’re burning gas — over and over again.
The Fix: Use constant for compile-time values. Use immutable for values that are set once at deployment. Both are much cheaper at runtime.
// Inefficient - These values are read from expensive storage slots every time they're used.Here’s the thing: gas optimization is great — until it isn’t. If you’re chasing micro-savings at the cost of security or clarity, you’re heading into dangerous territory.
Solidity’s compiler has a built-in optimizer that can shrink your bytecode and improve gas performance.
If your contract’s going to be used heavily — like in a token or protocol — go with 20000. It costs more to deploy, but saves users money on every interaction.
ConclusionWriting smart contracts that just “work” isn’t enough anymore. You’ve got to think about gas — because your users definitely will. With just a few smart choices, you can make your contracts cheaper to run and friendlier to the people using them.
These five patterns are a great starting point. Use gas reporters like Hardhat or Foundry to measure improvements. Keep your code clear, safe, and optimized.
Final reminder: Readable code wins. Safe code wins. And then — optimize.Have questions or ideas?
Contact us at: [email protected]
explore more: www.ancilar.com
5 Gas Optimization Patterns Every Solidity Developer Must Master was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.