# Gas Saving Technique 21: Unneeded If statements

{% hint style="info" %}
[**Book an audit with Zokyo**](https://www.zokyo.io/)
{% endhint %}

#### Introduction

Every operation in a smart contract costs gas. To optimize for lower gas consumption, it is crucial to remove code that doesn't contribute to the contract’s functionality. One common area of improvement is the removal of unnecessary `if` statements that check conditions already verified elsewhere or conditions that can never be true. This tutorial will guide you through identifying and removing such redundant `if` statements.

#### Impact & Details

**Understanding the Impact:**

* **Gas Savings**: Unnecessary `if` statements waste gas each time the contract is called, and removing them can lead to gas savings over the lifetime of the contract.
* **Code Simplification**: Redundant checks complicate the codebase. Removing them simplifies the code, making it easier to read and maintain.

#### Example: Removing Unneeded 'If' Statements

**Before Optimization:**

```solidity
solidityCopy codepragma solidity ^0.8.0;

contract ExampleContract {
    function cleanUp(bytes32 claimIdentifier) external {
        if (_setState(claimIdentifier, State.NonExistent) != State.Cleaned) {
            revert InvalidState();  // This check might be unnecessary
        }
        // ... rest of the function ...
    }
}
```

**After Optimization:**

Analyzing the `_setState` function and understanding the various states a `claimIdentifier` can be in will often reveal that some checks are unnecessary. If you find that `_setState(claimIdentifier, State.NonExistent)` can only result in `State.Cleaned` or revert beforehand, you can safely remove the `if` statement.

```solidity
solidityCopy codepragma solidity ^0.8.0;

contract ExampleContract {
    function cleanUp(bytes32 claimIdentifier) external {
        _setState(claimIdentifier, State.NonExistent);
        // The unnecessary 'if' statement has been removed
        // ... rest of the function ...
    }
}
```

#### Recommended Mitigation Steps

1. **Review and Analyze**: Review the code thoroughly to identify `if` statements that check for conditions already verified elsewhere or that will never be true.
2. **Remove Redundant Checks**: After identification, safely remove these unnecessary `if` statements from the code.
3. **Test**: Always test the contract after making changes to ensure that it still behaves as expected.

#### Conclusion

Removing unneeded `if` statements is a simple yet effective optimization that can lead to gas savings and a cleaner, more manageable codebase. Always be sure to understand the logic and states of your contract thoroughly to safely remove these checks without introducing new risks, and test your contract extensively to ensure it behaves as expected after the changes.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zokyo-auditing-tutorials.gitbook.io/zokyo-gas-savings/tutorials/gas-saving-technique-21-unneeded-if-statements.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
