Zokyo Gas Savings
  • ⛽Zokyo Gas Savings
  • 📚Tutorials
    • ✔️Gas Saving Technique 1: Unchecked Arithmetic
    • ⛓️Gas Saving Technique 2: Immutable Variable
    • ✨Gas Saving Technique 3: Double star ** inefficiency
    • 💰Gas Saving Technique 4: Cache Array Length
    • ⬅️Gas Saving Technique 5: ++i costs less gas compared to i++
    • ⚖️Gas Saving Technique 6: NOT operator ! cheaper than boolean FALSE
    • 🪡Gas Saving Technique 7: Using Short Reason Strings
    • 🪵Gas Saving Technique 8: Use Custom Errors instead of Revert Strings to save Gas
    • ✒️Gas Saving Technique 9: Use Custom Errors instead of Revert Strings to save Gas
    • 👾Gas Saving Technique 10: Calldata cheaper than memory
    • ⛔Gas Saving Technique 11: > 0 is less efficient than != 0 for unsigned integers
    • ➗Gas Saving Technique 12: SafeMath no longer needed
    • 😮Gas Saving Technique 13: variables default to 0
    • 🧱Gas Saving Technique 14: struct layout/ variable packing
    • 📞Gas Saving Technique 15: Cache External Call
    • ✍️Gas Saving Technique 16: Early Validation before external call
    • 😎Gas Saving Technique 17: Don’t cache value that is used once
    • 😧Gas Saving Technique 18: Redundant code
    • ✅Gas Saving Technique 19: Early Validation before external call
    • ⛏️Gas Saving Technique 20: Storage vs Memory read optimizations
    • ✒️Gas Saving Technique 21: Unneeded If statements
    • 🌗Gas Saving Technique 22: >= is cheaper than >
    • 🎒Gas Saving Technique 23: Public to private constants
    • ⏹️Gas Saving Technique 24: Make unchanged variables constant/immutable
    • ⏱️Gas Saving Techniques 25: Redundant Access Control Checks
    • ➡️Gas Saving Technique 26: Shift Right instead of Dividing by 2
    • 🪃Gas Saving Tutorial 27: Efficient Boolean Comparison
    • 🤝Gas Saving Technique 28: && operator uses more gas
    • 👓Gas Saving Technique 29: x = x + y is cheaper than x += y
    • 👂Gas Saving Technique 30: Using 1 and 2 rather than 0 and 1 saves gas
    • ⚽Gas Saving Technique 31: Optimize Storage by Avoiding Booleans
    • 🔙Gas Saving Technique 32: Optimal Use of Named Return Variables in Solidity
    • 🛢️Gas Saving Technique 33: Making Functions Payable for Optimized Gas Costs
    • ✍️Gas Saving Technique 34: Optimizing Storage References in Smart Contracts
    • ⛰️Gas Saving Technique 35: Usage of uints/ints smaller than 32 bytes (256 bits) incurs overhead
    • 🌪️Gas Saving Technique 36: Inlining Single Use Internal Functions for Savings
    • ☄️Gas Saving Technique 37: Switching from Public to External Functions for Savings
    • 🎆Gas Saving Technique 38: Upgrading Solidity Compiler to Improve Gas Efficiency and Security
    • 🕶️Gas Saving Technique 39: Avoiding Duplicated Code for Gas Savings
    • 😄Gas Saving Technique 40: Removal of Unused Internal Functions for Gas Savings
    • 🖋️Gas Saving Tutorial 41: In-lining Single Use Modifiers For Gas Saving
    • ⛏️Gas Saving Technique 42: `require` vs`assert`
Powered by GitBook
On this page
  1. Tutorials

Gas Saving Technique 18: Redundant code

PreviousGas Saving Technique 17: Don’t cache value that is used onceNextGas Saving Technique 19: Early Validation before external call

Last updated 1 year ago

Introduction

When crafting smart contracts, writing efficient and lean code is paramount. Redundant code, or code that doesn't contribute to the functionality, inflates the gas cost without providing any value. In this tutorial, we will discuss how to identify and remove redundant code to optimize gas usage.

Impact & Details

Understanding Redundancy

  • Gas Cost Impact: Each line of code in a smart contract consumes gas when deployed and executed. Redundant code increases gas costs unnecessarily, putting an extra burden on users.

  • Code Maintainability: Redundant code makes the contract more complex and harder to maintain or audit. Removing unnecessary code simplifies the contract, making it easier to read and understand.

Example: Removing Redundant Code

Below is an example illustrating redundant code and its optimized version:

Before Optimization:

solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyContract {
    uint256 public value;

    function setValue(uint256 _value) public {
        require(_value > 0, "Value must be positive");
        value = _value;
        value = value;  // Redundant code
        // ... rest of the function ...
    }
}

After Optimization:

solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyContract {
    uint256 public value;

    function setValue(uint256 _value) public {
        require(_value > 0, "Value must be positive");
        value = _value;
        // ... rest of the function ...
    }
}

Recommended Mitigation Steps

  1. Review & Identify: Go through the smart contract to identify and list down sections of redundant code. Look for lines that don't alter the contract state or affect the flow of execution.

  2. Remove Redundant Code: Once identified, safely remove the redundant code sections without affecting the overall functionality.

  3. Testing: After removal, conduct thorough testing to ensure that the contract operates as expected with the redundant code removed.

Conclusion

Removing redundant code is a straightforward practice that yields gas savings and creates cleaner, more maintainable contracts. Every line of code should purposefully contribute to the contract’s functionality. After implementing these changes, meticulously test the contract to ensure it works as intended while being more gas-efficient.

📚
😧
Book an audit with Zokyo