One rule
Types say what a value is. Data location says where complex data lives while the function runs.
In Solidity 0.8.x, be explicit. A recruiter should understand your contract state by reading the variable declarations.
Types you will use
Solidity is statically typed. Every state variable and local variable needs a type. There is no null or undefined, so new variables start with a default value.
Common defaults are 0 for uint256, false for bool, the zero address for address, and an empty value for string or bytes.
Readable contract state
This example stores student project submissions. It uses simple types, a struct for grouped data, a mapping for lookup, and an array for public listing.
Use this pattern when your hackathon contract needs records, owners, votes, tasks, certificates, or payments.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ProjectRegistry {
uint256 public constant MAX_TITLE_LENGTH = 80;
address public immutable admin;
struct Project {
address builder;
string title;
string repoUrl;
bool submitted;
}
mapping(address => Project) public projectsByBuilder;
address[] public builders;
constructor() {
admin = msg.sender;
}
function submitProject(string calldata title, string calldata repoUrl) external {
require(bytes(title).length > 0, "Title required");
require(bytes(title).length <= MAX_TITLE_LENGTH, "Title too long");
require(!projectsByBuilder[msg.sender].submitted, "Already submitted");
projectsByBuilder[msg.sender] = Project({
builder: msg.sender,
title: title,
repoUrl: repoUrl,
submitted: true
});
builders.push(msg.sender);
}
}Pick the right type
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
Storage, memory, calldata
Storage is permanent contract state. It stays between transactions and costs more to read and modify, so store only what the contract must remember.
Memory is temporary and cleared after the call. Calldata is also temporary, but it is the read-only input area for external function parameters.
Use calldata first
For external functions that receive strings, bytes, arrays, or structs and do not need to edit them, use calldata.
If you need to change a copy inside the function, use memory. If you need to update contract state, write to storage.
Avoid old tutorial habits
Do not add pragma experimental ABIEncoderV2 in a Solidity 0.8.x project. ABI coder v2 is already the default, and current Solidity warns that this pragma is deprecated.
Do not store private secrets in variables. Everything in a smart contract is publicly visible, including private state variables and local variables.
Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done