Solidity Crash Course - Part 05 π
Introduction π
Welcome back to our Solidity crash course! In this part, we'll cover some essential concepts to help you write more efficient and structured smart contracts. Let's dive in! πββοΈ
Solidity - While Loops π
Loops are used to execute a block of code multiple times based on a condition. A while loop continues to execute as long as the condition remains true
.
Example:
pragma solidity ^0.8.0;
contract WhileLoopExample {
uint public count;
function countUp(uint _limit) public {
while (count < _limit) {
count++;
}
}
}
β Why use while loops?
- Great for situations where you donβt know the exact number of iterations beforehand.
β οΈ Best Practice: Be cautious of infinite loops as they can waste gas and cause transactions to fail.
Solidity - Events π’
Events allow smart contracts to communicate with external applications like dApps. They help log important actions that occur on the blockchain.
Example:
pragma solidity ^0.8.0;
contract EventExample {
event NumberChanged(uint oldValue, uint newValue);
uint public number;
function updateNumber(uint _newValue) public {
emit NumberChanged(number, _newValue);
number = _newValue;
}
}
β Why use events?
- Efficiently store data without consuming unnecessary gas.
- Helps external applications listen to changes.
β οΈ Best Practice: Use events to log only important updates to reduce gas costs.
Solidity - For Loops π
A for loop is used when you know the number of iterations beforehand.
Example:
pragma solidity ^0.8.0;
contract ForLoopExample {
uint[] public numbers;
function fillArray(uint _limit) public {
for (uint i = 0; i < _limit; i++) {
numbers.push(i);
}
}
}
β Why use for loops?
- Ideal when you need to iterate a known number of times.
β οΈ Best Practice: Avoid looping through large arrays to prevent exceeding gas limits.
Solidity - Arrays π¦
Arrays store multiple values in a single variable, allowing efficient data management.
Example:
pragma solidity ^0.8.0;
contract ArrayExample {
uint[] public numbers;
function addNumber(uint _num) public {
numbers.push(_num);
}
function getNumber(uint index) public view returns (uint) {
return numbers[index];
}
}
β Why use arrays?
- Efficient storage and retrieval of multiple data points.
- Useful for managing lists like registered users or transaction records.
β οΈ Best Practice: Use mappings instead of arrays when searching for values frequently.
Solidity - Struct ποΈ
Structs allow you to create custom data types, grouping multiple properties together.
Example:
pragma solidity ^0.8.0;
contract StructExample {
struct Person {
string name;
uint age;
}
Person public user;
function setUser(string memory _name, uint _age) public {
user = Person(_name, _age);
}
}
β Why use structs?
- Helps organize related data efficiently.
- Useful for defining complex objects like user profiles or product details.
β οΈ Best Practice: Keep struct sizes minimal to optimize gas usage.
Conclusion π―
We covered essential Solidity concepts like loops, events, arrays, and structs. These are fundamental for writing more structured and efficient smart contracts.
Let me know if you have any questions, comments, or feedback! Would love to help you out! π
Top comments (0)