Naming Convention

A naming convention is a set of rules for choosing names for variables, functions, and other important entities in your code. With respect to solidity code, here are some guidelines for the naming convention while writing your code.

  • Use CamelCase for naming variables and functions (e.g. helloGeeks)
  • Use PascalCase for contract and struct names.
  • Use descriptive, lowercase names for variables and functions (e.g. totalSupply instead of ts).
  • Use a leading underscore for private variables (e.g. _privateVariable).
  • Use a leading is or has for boolean variables (e.g. isActive or hasValue).
  • Use descriptive and meaningful names for constants, and use ALL_CAPS for naming them.
  • Avoid abbreviations and single-letter names, except for common abbreviations like “id” and “num”.
  • Avoid using reserved words as names.
  • Use descriptive and meaningful names for events, and prefix the name with Log.
  • Use descriptive and meaningful names for functions, and indicate their purpose in the name.
  • Use descriptive and meaningful names for function arguments, and indicate the purpose of the argument in the name.
  • Use meaningful names for local and state variables, and indicate their purpose in the name
  • Use descriptive and meaningful names for modifiers, and indicate their purpose in the name.
  • Use descriptive and meaningful names for enums, and indicate their purpose in the name.

Refer to the code snippet attached ahead for reference.

contract MyContract {

// Constants

uint256 public constant MIN_VALUE = 10;

uint256 public constant MAX_VALUE = 100;

// Variables

uint256 public totalSum;

address public owner;

mapping(address => uint256) public balances;

Solidity – Style Guide

Solidity is a computer programming language used to create Ethereum smart contracts. These contracts self-execute. The code and the agreements contained therein are enforced by the blockchain network. Solidity is a high-level language, meaning that it is designed to be human-readable and easy to write. It is based on the JavaScript programming language and has a syntax similar to C++. Solidity is a statically-typed language, meaning that variables have a fixed type that must be declared when they are created. It also supports complex data types such as arrays and structs (collections of related variables).
One of the main benefits of Solidity is that it allows developers to build decentralized applications (DApps) on the Ethereum platform. These DApps can be used for a wide range of applications, including supply chain management, voting systems, and financial applications.

In this style guide, we will cover several elements starting with code layout till NatSpec declaration in code, followed by an example for better understanding.

Similar Reads

Code Layout

Code layout refers to the way that your solidity code is structured and formatted. Proper code layout helps to make your code more readable, maintainable, and easy to understand. With respect to solidity code, here are some guidelines to maintain an ideal code layout throughout your code....

Order of layout

Order of layout refers to the recommended order in which different elements of a solidity contract should be organized. It is important to follow a consistent order of layout in order to make the code more readable and easier to understand. This order might vary slightly for different programming languages. This order consists of several optional and mandatory pointers. We’ve tried to cover almost all (both mandatory/optional) elements as per their order of occurrences in the solidity code....

Naming Convention

A naming convention is a set of rules for choosing names for variables, functions, and other important entities in your code. With respect to solidity code, here are some guidelines for the naming convention while writing your code....

NatSpec

NatSpec (Natural Language Specification) is a system for documenting solidity code in a way that is easy for humans to understand. NatSpec comments are written in Markdown format and can be accessed through the Ethereum NatSpec Standard JSON Interface. To add single-line NatSpec documentation to your Solidity code, use double-slash comments (‘//’) followed by the NatSpec tag and the documentation text. And for adding multiple comments (“/**…..*/”) Here’s an example to refer to while writing comments....