Variable names don't follow the Solidity style guide
Learn why variable names that don't follow the Solidity style guide can lead to security issues. Understand the importance of using CONSTANT_CASE for constant variable names.
Category
non-critical
Languages
solidity
Analysis Layer
static
Severity
info
In Solidity, it is important to follow the style guide for variable names to ensure code readability and maintainability. One common issue is variable names that do not adhere to the recommended naming convention.
For constant variables, it is recommended to use all capital letters, with underscores separating each word. This convention, known as CONSTANT_CASE, helps distinguish constants from regular variables and makes them easier to identify.
Let's consider an example where the variable names don't follow the Solidity style guide:
pragma solidity ^0.8.0;
contract VariableNaming {
uint totalSupply; // regular variable
uint constant initialAmount = 100; // constant variable
string constant contractName = "ExampleContract"; // constant variable
address constant owner = address(0x123456789); // constant variable
}
As we can see, the variable names initialAmount, contractName, and owner do not follow the recommended naming convention.
To adhere to the Solidity style guide, we should update the constant variable names to use CONSTANT_CASE:
pragma solidity ^0.8.0;
contract VariableNaming {
uint totalSupply; // regular variable
uint constant INITIAL_AMOUNT = 100; // constant variable
string constant CONTRACT_NAME = "ExampleContract"; // constant variable
address constant OWNER = address(0x123456789); // constant variable
}
By following the recommended naming convention, it becomes easier to distinguish between regular and constant variables in the codebase.
Remember, adhering to the Solidity style guide not only improves code readability but also contributes to writing more maintainable and secure contracts.
Don't forget to review the official Solidity style guide for more recommendations on writing clean and readable Solidity code.