😎Gas Saving Technique 17: Don’t cache value that is used once
Introduction
Impact & Details
Example: Avoiding Unnecessary Variable Caching
solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyContract {
struct Claim {
uint256 updated;
// ... other attributes ...
}
mapping(bytes32 => Claim) public claims;
function someFunction(bytes32 claimIdentifier) public {
Claim storage claim = claims[claimIdentifier];
uint256 timestamp = claim.updated; // Unnecessary variable caching
// timestamp is used only once in the function
// ... rest of the function ...
}
}Recommended Mitigation Steps
Conclusion
PreviousGas Saving Technique 16: Early Validation before external callNextGas Saving Technique 18: Redundant code
Last updated