Learning Objectives – after this chapter you will be able to:
  • Write the SPDX licence identifier and pragma lines correctly.
  • Read version ranges such as ^0.8.20 and >=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.0 or Apache-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
  • pragma is 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.
PragmaMeaning
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 comment and /* 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), @param and @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)
ElementStyleExample
Contracts, structs, events, enumsCapWordsVotePanel, Candidate
Functions, variables, modifiersmixedCasetotalSupply, onlyOwner
ConstantsUPPER_CASE_WITH_UNDERSCORESMAX_SUPPLY
Indentation4 spaces per level–
Key Takeaways
  • Start every file with the SPDX licence line and a version pragma.
  • ^0.8.20 allows 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.
Download | Print      E-Notes

The PDF E-Notes are the earlier printable version of this chapter. The notes on this page are the latest, revised version.

Video : Solidity Basic Syntax
Resource File
Question / Answer (15 questions)

Answer : A pragma is a compiler directive. The version pragma, e.g. pragma solidity ^0.8.20;, states which compiler versions are allowed to compile the file.

Answer : The file can be compiled with version 0.8.20 or any later 0.8.x version, but not with 0.9.0 or higher.

Answer : It allows any compiler version from 0.8.2 up to, but not including, 0.9.0.

Answer : No. A version pragma applies only to the source file in which it is written; every imported file has its own pragma.

Answer : SPDX (Software Package Data Exchange) is an open standard (ISO/IEC 5962:2021) for communicating licence information. Solidity files start with a comment such as // SPDX-License-Identifier: MIT to declare their licence in a machine-readable way.

Answer : The compiler issues a warning (not an error). The contract still compiles, but adding the identifier is recommended, especially for published source code.

Answer : UNLICENSED, i.e. // SPDX-License-Identifier: UNLICENSED.

Answer : A contract is a collection of code (functions) and data (state) that resides at a specific address on the blockchain. It is similar to a class in object-oriented programming.

Answer : No. Ethereum has no built-in cron or scheduler; a contract function runs only when a transaction or another contract calls it. Off-chain automation services must send that call.

Answer : import "./Token.sol"; imports all symbols, and import {Token} from "./Token.sol"; imports only the named symbol.

Answer : NatSpec comments use /// or /** */ with tags such as @title, @author, @notice, @dev, @param and @return to document contracts and functions for users, developers and tools.

Answer : @notice explains to end users what a function does; @dev gives extra technical details for developers.

Answer : Contract names use CapWords (e.g. VotePanel); function and variable names use mixedCase (e.g. totalSupply).

Answer : In upper case with underscores, for example MAX_SUPPLY.

Answer : The call switches execution context to the other contract, so the caller’s state variables are not directly accessible inside the called contract; data must be passed as arguments.
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.

Q1. Which line is normally the first line of a Solidity file?

Show Answer

Correct Answer : A. // SPDX-License-Identifier: MIT
The SPDX licence comment comes first, followed by the pragma.

Q2. Which compiler version is NOT allowed by pragma solidity ^0.8.20;?

Show Answer

Correct Answer : D. 0.9.0
The caret allows later 0.8.x versions but not 0.9.0.

Q3. SPDX identifiers describe a file’s:

Show Answer

Correct Answer : B. Software licence
SPDX is a standard for licence information.

Q4. Which comment style is used for NatSpec documentation?

Show Answer

Correct Answer : C. /// comment
NatSpec uses /// or /** */ comments.

Q5. According to the Solidity style guide, a constant should be named:

Show Answer

Correct Answer : D. MAX_SUPPLY
Constants use UPPER_CASE_WITH_UNDERSCORES.

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.

  1. 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
  2. Solidity Team. (n.d.). Structure of a Contract. Solidity Documentation. https://docs.soliditylang.org/en/latest/structure-of-a-contract.html
  3. Solidity Team. (n.d.). NatSpec Format. Solidity Documentation. https://docs.soliditylang.org/en/latest/natspec-format.html
  4. Solidity Team. (n.d.). Style Guide. Solidity Documentation. https://docs.soliditylang.org/en/latest/style-guide.html
  5. The Linux Foundation. (n.d.). SPDX License List. https://spdx.org/licenses/
  6. The Linux Foundation. (n.d.). About SPDX (ISO/IEC 5962:2021). https://spdx.dev/about/
  7. 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/
  8. 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.