<x> += <y>
Costs More Gas Than <x> = <x> + <y>
For State Variables
x = x + y
costs less gas than x += y
. For example, balanceOf[to] += amount
can be changed to balanceOf[to] = balanceOf[to] + amount
Instances:
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L155
s_collateralDeposited[msg.sender][tokenCollateralAddress] += amountCollateral;
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L198
s_DSCMinted[msg.sender] += amountDscToMint;
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L356
totalCollateralValueInUsd += getUsdValue(token, amount);
Use of X = X + Y
instead of X += Y
saves gas.
Manual review and VS Code
Use X = X + Y
wherever possible instead of X += Y
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.