- Write the SPDX licence identifier and pragma lines correctly.
- Read version ranges such as
^0.8.20and>=0.8.2 <0.9.0. - Import other files, write comments and NatSpec documentation.
- Recognise the overall layout of a contract and follow naming conventions.
Layout of a Solidity Source File
// SPDX-License-Identifier: MIT // 1. licence identifier
pragma solidity ^0.8.20; // 2. compiler version
import "./Ownable.sol"; // 3. imports (optional)
/// @title A simple counter // 4. NatSpec documentation
/// @author Ishaanvi InfoSoft
contract Counter is Ownable { // 5. contract definition
uint256 public count; // state variable
/// @notice Increase the counter by one
function increment() public { // function
count += 1; // every statement ends with ;
}
}
Illustration of the file layout (it imports a file named Ownable.sol, so it compiles only when that file exists).
SPDX License Identifier
- SPDX stands for Software Package Data Exchange, an open standard (ISO/IEC 5962:2021) maintained by the Linux Foundation for communicating software licence information.
- The SPDX License List gives each common open-source licence a standard short identifier, such as
MIT,GPL-3.0orApache-2.0. - Because contract source code is often published (for example for verification on block explorers), every Solidity file should begin with a machine-readable comment such as
// SPDX-License-Identifier: MIT. - If the code is not open source, use
// SPDX-License-Identifier: UNLICENSED. The compiler shows a warning when the line is missing; it stores the identifier in the contract metadata but does not check the licence itself.
Pragma – Choosing the Compiler Version
pragmais a directive to the compiler. The version pragma states which compiler versions may compile the file, protecting code from being compiled by an incompatible version.- Solidity is still evolving and new versions add features and fixes, so pinning a version range is important. A pragma applies only to the file in which it is written.
- It is good practice to place the pragma right after the SPDX line.
| Pragma | Meaning |
|---|---|
pragma solidity 0.8.26; | Exactly version 0.8.26. |
pragma solidity ^0.8.20; | 0.8.20 or any later 0.8.x version, but not 0.9.0 (caret = “compatible versions”). |
pragma solidity >=0.8.2 <0.9.0; | Any version from 0.8.2 up to (but not including) 0.9.0 – the style used in Remix templates and this course’s projects. |
Importing Other Files
import "./Token.sol"; // import everything from a file
import {Token, IToken} from "./Token.sol"; // import selected symbols (recommended)
import * as Lib from "./MathLib.sol"; // import everything under a name
In Remix, files live in the workspace, so the course projects use paths such as import "contracts/CandidateMaster.sol";.
Comments and NatSpec
// single-line commentand/* multi-line comment */are ignored by the compiler.- NatSpec (Ethereum Natural Language Specification) comments use
///or/** ... */with tags such as@title,@author,@notice(for users),@dev(for developers),@paramand@return. Tools and wallets can display these descriptions.
Contracts
- A contract in Solidity is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain.
- Contracts are similar to classes in object-oriented languages: they hold persistent data in state variables and provide functions that read or modify them.
- Calling a function of a different contract performs an EVM message call and switches context, so the caller’s state variables are not directly accessible inside the called contract.
- Nothing happens on its own: a contract’s functions run only when a transaction or another contract calls them. There is no built-in “cron” in Ethereum to run a function automatically at a set time.
Naming Conventions (Solidity Style Guide)
| Element | Style | Example |
|---|---|---|
| Contracts, structs, events, enums | CapWords | VotePanel, Candidate |
| Functions, variables, modifiers | mixedCase | totalSupply, onlyOwner |
| Constants | UPPER_CASE_WITH_UNDERSCORES | MAX_SUPPLY |
| Indentation | 4 spaces per level | – |
Key Takeaways
- Start every file with the SPDX licence line and a version pragma.
^0.8.20allows 0.8.20 and newer 0.8.x releases, but not 0.9.0.- A contract bundles state and functions at an address; it only runs when called.
The PDF E-Notes are the earlier printable version of this chapter. The notes on this page are the latest, revised version.
Question / Answer (15 questions)
pragma solidity ^0.8.20;, states which compiler versions are allowed to compile the file.// SPDX-License-Identifier: MIT to declare their licence in a machine-readable way.UNLICENSED, i.e. // SPDX-License-Identifier: UNLICENSED.import "./Token.sol"; imports all symbols, and import {Token} from "./Token.sol"; imports only the named symbol.MAX_SUPPLY.MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- Solidity Team. (n.d.). Layout of a Solidity Source File (SPDX, pragmas, imports, comments). Solidity Documentation. https://docs.soliditylang.org/en/latest/layout-of-source-files.html
- Solidity Team. (n.d.). Structure of a Contract. Solidity Documentation. https://docs.soliditylang.org/en/latest/structure-of-a-contract.html
- Solidity Team. (n.d.). NatSpec Format. Solidity Documentation. https://docs.soliditylang.org/en/latest/natspec-format.html
- Solidity Team. (n.d.). Style Guide. Solidity Documentation. https://docs.soliditylang.org/en/latest/style-guide.html
- The Linux Foundation. (n.d.). SPDX License List. https://spdx.org/licenses/
- The Linux Foundation. (n.d.). About SPDX (ISO/IEC 5962:2021). https://spdx.dev/about/
- O’Reilly Learning Platform. (n.d.). Solidity Programming Essentials – chapter on pragma (online edition of Modi, 2018). https://www.oreilly.com/library/view/solidity-programming-essentials/9781788831383/
- Modi, R. (2018). Solidity Programming Essentials: A beginner’s guide to build smart contracts for Ethereum and blockchain. Packt Publishing. ISBN 978-1-78883-138-3.