Hi @vale_dinap,
You’re very welcome, and I’m glad you found my feedback helpful
You’ve made a good job of amending your contract
This is a very good question, and also an important one to consider when coding Ethereum smart contracts in Solidity, because usually additional code means more low level operations and therefore higher transaction costs in terms of gas consumption. Obviously security, utility and functionality should not be compromised just to save gas; however, generally speaking, transaction costs will be lower if we only add the code that is necessary, and aim to code as efficiently and concisely as possible. Avoiding redundant code can also help to reduce the risk of bugs.
It’s perfectly acceptable for Solidity functions not to return any values; and if they don’t need to, then it’s better practice that they don’t, for the reasons I’ve just outlined above. The possible future utility of a piece of code also isn’t really a factor with smart contracts, because once they are deployed they cannot be modified. So, any code that is redundant when the contract is deployed is likely to remain redundant.
Our Bank contract, as it is, doesn’t actually need the deposit or withdraw functions to return the updated balances, and so the return statements in the function bodies, and returns(uint)
in the function headers, can be removed. We already have the getBalance function which can be called separately to consult the individual account holder’s updated balance after any transaction (a deposit, withdrawal or transfer); and getBalance is a view function, which doesn’t generate a transaction, and so doesn’t consume any gas.
You can get an indication of the reduction in gas costs by deploying your Bank contract in Remix and then making a deposit and a withdrawal (i) with the code in both functions that returns the updated balances, and (ii) after removing this code from both functions. After each transaction (contract deployment, deposit, withdrawal) you can see the associated gas cost within the transaction receipt generated in the Remix console.
I say that it’s an indication of the gas cost, because I’m aware that these gas costs in Remix may not be entirely accurate. You’ll notice that Remix generates two gas cost figures: transaction cost and execution cost, and I still haven’t entirely got to the bottom of what the difference is, and whether there is any double counting or other aspects that need to be taken into consideration. These figues used to be different amounts, but I’ve now noticed they are the same! It’s also possible to click on “Debug” next to a transaction receipt, which will take you to the Debugger in the panel on the left. Here you can click through each of the separate low-level operations performed in that transaction and see the gas consumed for each one. I’ve used that before to identify the gas consumption for each individual, low-level operation performed for a specific chunk of code. By adding those up, I’ve tried to arrive at the total gas consumed for alternative chunks of code, in order to compare how much gas they use. But, as I’m sure you can imagine, this gets quite time consuming!
A more straight forward way to arrive at the gas consumption is to deploy the contract using Truffle and Ganache, and then perform various transactions using the Truffle console. You will learn how to use these tools in the 201 course, which follows this one.
Anyway, for now, the gas cost figures in Remix will at least prove to you that returning the updated balance increases the gas cost of all 3 transactions: contract deployment, deposit and withdrawal. The gas cost of deployment will be a one-off cost, whereas the gas cost associated with each function will be incurred each time they are executed.
Just let me know if you have any further questions.