> 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-23-public-to-private-constants.md).

# Gas Saving Technique 23: Public to private constants

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

**Introduction:**

Within a smart contract, the visibility of constants can have a subtle but notable impact on gas usage. This tutorial will guide you through the benefits of adjusting the visibility of constants from `public` to `private` or `internal` as a means of saving gas.

**Understanding the Impact:**

In Solidity, `public` variables have an automatically generated getter function, which, although useful, incurs extra gas cost during contract deployment. If a constant doesn't need to be accessed outside of the contract or derived contracts, changing its visibility can result in gas savings.

**Vulnerability Details:**

Deploying a contract with numerous `public` constants can unnecessarily increase its deployment gas cost due to the added getter functions. Moreover, every time these constants are accessed, additional gas is used.

**Example:**

**Using `public` constant:**

```solidity
solidityCopy codepragma solidity ^0.8.0;

contract GasSaver {
    // Public constant with generated getter function
    uint256 public constant SOME_VALUE = 12345;
}
```

**Optimized Version Using `internal` constant:**

```solidity
solidityCopy codepragma solidity ^0.8.0;

contract GasSaver {
    // Internal constant without generated getter function
    uint256 internal constant SOME_VALUE = 12345;
}
```

**Mitigation Steps:**

1. **Review Constants**: Examine your contract for `public` constants.
2. **Adjust Visibility**: If a constant doesn’t need to be accessed outside of the contract or by derived contracts, consider changing its visibility to `internal`. If it does not need to be accessed by derived contracts either, consider making it `private`.
3. **Test the Contract**: Ensure your contract still behaves as expected after making visibility adjustments.

**Benefits:**

* **Gas Efficiency**: Reducing visibility can save gas on contract deployment and when accessing constants.
* **Encapsulation**: It promotes better encapsulation and contract abstraction, enhancing contract security and readability.

**Consideration:**

* **Accessibility**: Ensure that no external component relies on the visibility of these constants before making changes.

**Conclusion:**

While the gas savings might be minor per operation, in contracts with high transaction volume or many constants, these savings accumulate. Additionally, this practice enhances the encapsulation within your contract, promoting a cleaner and more secure codebase. Always test thoroughly after making optimizations to ensure the contract's functionality remains intact.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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-23-public-to-private-constants.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.
