solidity related question of lesson-9 #2312
-
We have mentioned Or what happens when we've |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
The place where you initialize each variable determines when the initialization code will run. During initialization the code is executed in the following order:
If the initialization of each variable is independent, this makes no practical difference. You might simply get bytecode that is functionally equivalent but has some instructions ordered slightly differently. If variables depend on each other, you might actually get different results. In all of these places you can refer to other state variables and call arbitrary functions (even external ones). The function you call can have side-effects or modify state variables (even before they are initialized). To avoid problems that can arise from this it's best to keep things simple. Avoid dependencies and for declarations and base constructor arguments try to only use simple constants or functions that do not read or write other state variables. If you need to do something more complex, keep that logic inside the constructor body to make it easier to reason about what runs in what order. |
Beta Was this translation helpful? Give feedback.
-
@MasterofBlockchain there is little difference whatsoever if you initialize the variable with its value as a state variable compared to using the constructor to set the value. The slight difference comes with the little dynamic nature of setting the entranceFee value in our deploy script. Passing the value through the constructor make it slightly dynamic that we can easily change the value rather than tampering with the contract code any time there is a change. |
Beta Was this translation helpful? Give feedback.
@MasterofBlockchain there is little difference whatsoever if you initialize the variable with its value as a state variable compared to using the constructor to set the value. The slight difference comes with the little dynamic nature of setting the entranceFee value in our deploy script. Passing the value through the constructor make it slightly dynamic that we can easily change the value rather than tampering with the contract code any time there is a change.
However, it is also important to note that once the contract has been deployed we can't change anything even the entranceFee so it is just a matter of the developer's prefences. If you feel like you will set the fee to a specific v…