> For the complete documentation index, see [llms.txt](https://zokyo-auditing-tutorials.gitbook.io/zokyo-gas-savings/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zokyo-auditing-tutorials.gitbook.io/zokyo-gas-savings/tutorials/gas-saving-technique-39-avoiding-duplicated-code-for-gas-savings.md).

# Gas Saving Technique 39: Avoiding Duplicated Code for Gas Savings

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

**Introduction:** Duplicated code, also known as code redundancy, not only affects the readability and maintainability of smart contracts but also has a direct impact on deployment and execution costs. Each repeated line of code contributes to the overall size of a contract, leading to increased gas costs. This tutorial highlights the importance of avoiding duplicated code for gas efficiency.

***

**Concept**:

1. **Duplicated Code**: Refers to identical or very similar blocks of code that appear in multiple locations in a contract.
2. **Gas Efficiency**: The measure of how effectively a contract utilizes the gas required for deployment and transaction execution. Duplicated code negatively impacts gas efficiency by increasing the overall bytecode size.

***

**Reasons to Avoid Duplicated Code**:

1. **Increased Deployment Costs**: The larger the contract's bytecode, the more gas required to deploy it on the Ethereum network.
2. **Higher Transaction Costs**: Repeated operations can lead to higher execution costs for transactions.
3. **Maintenance Challenges**: Duplicated code can introduce bugs and make it harder to update or modify the contract in the future.
4. **Readability**: Redundant code makes it harder for developers to understand the contract's logic, potentially leading to errors or overlooked vulnerabilities.

***

**Techniques to Reduce Duplicated Code**:

1. **Modularization**:
   * Break your contract into smaller, reusable components or functions.
   * For example, if multiple functions use the same logic for checking user permissions, move that logic into a separate internal function.
2. **Use Libraries**:
   * Externalize common logic into libraries. This allows you to reuse code across different contracts.
   * Example: `SafeMath` library in Solidity for arithmetic operations.
3. **Inheritance and Interfaces**:
   * Use inheritance to create base contracts with shared logic that can be inherited by other contracts.
   * Define interfaces for contracts that can implement common functionality.
4. **State Variable Initialization**:
   * If multiple functions initialize the same state variables with default values, consider setting these defaults during contract deployment to avoid redundancy.

**Conclusion**: Avoiding duplicated code is essential for writing gas-efficient, readable, and maintainable smart contracts. Regularly reviewing and refactoring contracts to minimize redundancy can save developers both gas and potential headaches in the long run.
