A Solidity program has several main sections, as follows:
- Pragma: This tells Solidity what versions of the compiler are valid to compile this file.
- Comments: Developers should use comments for documenting code.
- Import: An import defines an external file that contains code that your smart contract needs.
- Contract(s): This section is where the body of your smart contract code resides.
Declaring valid compiler version in Ethereum smart contracts
Thepragma
directive should be the first line of code in a Solidity file. Because the Solidity language is still maturing, it is common for new compiler versions to include changes that would fail to compile older programs. The pragma
directive helps avoid compiler failures due to using a newer compiler.Here is the syntax for the pragma
directive:
pragma Solidity <<version number>>;Here is a sample
pragma
directive:
pragma Solidity ^0.4.24;
All statements in Solidity end with a semicolon.
The version number starts with a 0, followed by a major build number and a minor build number. For example, the version number 0.4.24 refers to major build 4 and minor build 24. The caret symbol (^) before the version number tells Solidity that it can use the latest build in a major version range. In the preceding example, Solidity can use a compiler from any build in the version 4 build range. This is a way to tell readers that your program was written for 0.4.24 but will still compile for subsequent version 4 builds.Although using the caret in the pragma
directive provides flexibility, it is a better practice to drop the caret and tell Solidity exactly what compiler version you expect.
Commenting your Solidity code
Adding comments to your code is an extra step that adds a professional look and feel to your Solidity code. A well-commented source code file is easier to read and understand and helps other developers quickly understand what your code is supposed to do. Even simple comments can cut down on the time required to fix bugs or add new functionality. Comments can also provide input for utilities to generate documentation for your smart contracts.You can use single-line or multiline regular comments. Single-line comments start with two forward slashes. Multiline comments start with the /*
characters and end with the */
characters. Here is an example of Solidity comments:
// Here is a single line Solidity comment /* I have a lot more to say with this comment, so I’ll use a multiline comment. The compiler will ignore everything after the opening comment characters, until it sees the closing comment characters. */A third type of Solidity comment is called the Ethereum Natural Specification (NatSpec) directive. You can use NatSpec to provide information about your code for documentation generators to use to create formatted documentation the describes your smart contracts. NatSpec directives start with three forward slashes and include special tags with data for the documentation. Here is an example of using NatSpec directives:
/// @title Greeter smart contract /// @author Joe Programmer /// @notice This code takes a person's name and says hello /// @param name The name of the caller /// @return greeting The greeting with the caller's name
Check out NatSpec documentation for additional information.
Importing external code into your Ethereum smart contract
The import section is optional but can be powerful when used correctly in your Ethereum smart contract. If your smart contract needs to refer to code in other files, you’ll have to import those other files first. Importing files makes it as though you copied the other code into the current file. Using imports helps you avoid actually copying code from one place to another. If you need to access code, just import the Solidity file that contains it.The syntax for importing other files is simple. You use the import
keyword and then provide the filename for the file you want to import. For example, to import the file myToken.sol, use this syntax:
Import 'myToken.sol';
Defining your Ethereum smart contracts
In the last main section of Solidity, you define the contents of your smart contract. It starts with the keywordcontract
and contains all of the functional code in your smart contract. You can have multiple contract
sections in Solidity. That means a single .sol file can define multiple contracts. Here is an example contract
section:
contract HelloWorld { string private helloMessage = "Hello world"; function getHelloMessage() public view returns (string) { return helloMessage; } }Inside the
contract
section is where you define all of your variables, structures, events, and functions. There's a lot more to the contract
section of your code, but for now, you know how to set up a Solidity smart contract.Once you master the basics of Solidity, you can continue to develop more complex code and the sky's the limit.