# Gas Saving Technique 13: variables default to 0

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

#### Introduction

In the realm of smart contract development, every gas unit saved is crucial. One straightforward yet often overlooked optimization technique is avoiding redundant initialization of variables. By understanding and leveraging the default values of variable types in Solidity, developers can write cleaner code and achieve minor gas savings.

#### Impact & Details

**Understanding Gas Consumption**

* **Redundant Initialization Cost**: Initializing variables to their default values, like setting `bool` variables to `false` or `uint256` variables to `0`, introduces unnecessary operations and thereby consumes extra gas.
* **Default Values**: In Solidity, uninitialized `bool` variables default to `false`, and `uint256` variables default to `0`. Being aware of these defaults allows for cleaner and more gas-efficient code.

#### How to Avoid Redundant Initialization for Gas Savings

**Practical Example: Removing Redundant Initialization**

Consider the following smart contract snippet before and after optimization:

Before Optimization:

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

contract MyContract {
    bool transferSuccess = false;  // Redundant initialization
    uint256 public periodFinish = 0;  // Redundant initialization
    uint256 public rewardRate = 0;  // Redundant initialization
}
```

After Optimization:

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

contract MyContract {
    bool transferSuccess;  // Defaults to false
    uint256 public periodFinish;  // Defaults to 0
    uint256 public rewardRate;  // Defaults to 0
}
```

In the optimized version, redundant initializations are removed, and the contract relies on default values, resulting in slightly less gas consumption and cleaner code.

#### Recommended Mitigation Steps

1. **Identify Redundant Initializations**: Scan your smart contracts for variables being initialized to default values.
2. **Remove Redundant Initializations**: Omit explicit initialization for variables when you intend to set them to their default values.
3. **Test**: Conduct rigorous testing to ensure that the removal of explicit initializations does not affect the contract’s expected behavior while facilitating minor gas savings.

#### Conclusion

Avoiding redundant initializations by relying on variable default values is a straightforward and effective optimization technique for gas saving in smart contract development. Although the savings per transaction might be minor, the overall savings across many transactions can be more significant. After making these changes, always ensure to test the smart contract meticulously to ensure it functions as expected while consuming slightly less gas.


---

# 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-13-variables-default-to-0.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.
