Solidity Basics: Your First Lines of Code — Kudos board build (step 1/6)
Know how a Solidity file is structured.
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.
What to write
- SPDX line
add// SPDX-License-Identifier: MITas the first line.// SPDX-License-Identifier: MITDeclares the license in a machine-readable way. Most tooling and explorers expect this line; some compilers warn without it.
- Pragma
lock compiler withpragma 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.
- 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.
- 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.
- 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.
- Setter
function setGreeting(string calldata greeting) externalto 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.
- 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.