Is it bad practice to store variables to make cleaner code?
Solution 1:
All reference type variables (including struct
) within a function need to have a data location specified. There's no default value, so the compiler throws an error if you don't specify it.
Declaring the pointer to the storage variable and using it for one write operation is approx. 100 gas more expensive compared to performing the write directly to the storage variable. In a simple test (compiled without the optimizer option), I measured 65,562 gas writing to the deposits
mapping directly, compared to 65,664 (approx. 100 more) gas using the userAccount
storage pointer.
A usual transaction cost tens of thousands of gas, which makes the potential savings on multiple writes around one percent. But it really depends on your use case whether you want/need to optimize to this extent of savings and make the code less readable - or not.