Skip to content

Instantly share code, notes, and snippets.

@TehilaFavourite
Last active July 25, 2023 11:49
Show Gist options
  • Save TehilaFavourite/b9f9d8fbe7d1d4bf2768d94e1665dfb2 to your computer and use it in GitHub Desktop.
Save TehilaFavourite/b9f9d8fbe7d1d4bf2768d94e1665dfb2 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Bad approach: Unnecessary state updates
contract BadConditionalUpdate {
uint256 public counter;
function incrementCounter() public {
counter++; // Consumes gas for state update even if no condition is met
}
}
// Good approach: Conditionally update state
contract GoodConditionalUpdate {
uint256 public counter;
address public owner;
function incrementCounter() public {
if (msg.sender == owner) {
counter++; // State updated only if condition is met, saving gas
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment