Hi @Andrewg,
In the first line you define a local variable user
, but then you don’t reference it in the second line. So the first line is completely redundant and can, therefore, be removed. This leaves us with your second line, which clearly shows that your updateBalance function actually just does exactly the same thing as the addUser function:
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public {
users[id] = User(id, balance);
}
If we have two functions that perform exactly the same operation, that’s not efficient, and so in this case we might as well remove one of the functions and just call the remaining one something more general, such as update().
However, this is hardly ideal. So what you want to aim for with the updateBalance function, is code that only updates the balance
property for a particular user ID, and not the whole User
instance. We only need to create a whole new User
instance when we add a new user, because once added, their user ID won’t need to change (at least not at the same time as their balance).