Beginner

Solidity Basics: Your First Lines of Code — Kudos board build (step 1/6)

Know how a Solidity file is structured.

Developer Beginner
9/9 — Solidity Basics: Your First Lines of Code — Kudos board build (step 1/6)

GoalStart a tiny “KudosBoard” contract. Keep it lean: SPDX + pragma, one private string, and two functions to set and read a greeting. This compiles and deploys today.

Talk is cheap. Show me the code.
— Linus Torvalds

What to write

  1. SPDX line
    add // SPDX-License-Identifier: MIT as the first line.
    // SPDX-License-Identifier: MIT

    Declares the license in a machine-readable way. Most tooling and explorers expect this line; some compilers warn without it.

  2. Pragma
    lock compiler with pragma solidity ^0.8.24;.
    pragma solidity ^0.8.24;

    Pins a compiler range so builds and audits align. Prevents surprise behavior changes from newer compilers.

  3. Contract shell
    contract KudosBoard { … } to scope everything.
    contract KudosBoard {
        // ...
    }

    Defines the contract scope. Everything you write lives inside this block and becomes part of the ABI if marked public/external.

  4. State
    string private _greeting; so the surface stays intentional.
    string private _greeting;

    Private state forces callers through your functions. You control formatting, validation, and when changes are allowed.

  5. Constructor
    accept a string and assign _greeting = greeting;.
    constructor(string memory greeting) {
        _greeting = greeting;
    }

    Initializes storage at deploy time. Constructor args become part of the deploy transaction calldata.

  6. Setter
    function setGreeting(string calldata greeting) external to update state.
    function setGreeting(string calldata greeting) external {
        _greeting = greeting;
    }

    External means callable from outside; calldata avoids memory copies for inputs. This mutates storage and costs gas.

  7. Getter
    function greeting() external view returns (string memory) to read it.
    function greeting() external view returns (string memory) {
        return _greeting;
    }

    View marks it read-only; callers can read without gas when called locally. Returns a memory copy of the stored string.

Why SPDX/pragma lock license+compiler; private state keeps ABI intentional; explicit external/view + calldata reduce copies and make intent clear.

Try it Deploy this, call setGreeting(), then read greeting().

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract KudosBoard {
    string private _greeting;

    constructor(string memory greeting) {
        _greeting = greeting;
    }

    function setGreeting(string calldata greeting) external {
        _greeting = greeting;
    }

    function greeting() external view returns (string memory) {
        return _greeting;
    }
}

Next We’ll add per-user kudos and events in the following lessons.

← Previous section
Next lesson →