Last active
July 25, 2023 11:49
-
-
Save TehilaFavourite/b9f9d8fbe7d1d4bf2768d94e1665dfb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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