Lesson 14: why do we need function _initializeContract() ? #2323
-
Hi all, In this course's repo: RandomIpfsNft.sol, what is the need of having function _initializeContract(string[3] memory dogTokenUris) private {
if (s_initialized) {
revert AlreadyInitialized();
}
s_dogTokenUris = dogTokenUris;
s_initialized = true;
} Yes, it checks whether But, when, possibly, can such a situation occur where the URI array already has the values and someone tried to pass the values again and it got reverted ?? I believe we can never set the So, why doesn't a simple statement: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
@ManuWeb3 : Yes, and In course tutorial Patrick precisely used the method you mentioned:
|
Beta Was this translation helpful? Give feedback.
-
We need it because we need the contract to be initialized with 3 dogTokenUris. How so? It is done so in the parameterized constructor: Also, the conditional further checks for whether if we have already initialized the given contract already or not. How does the conditional work? Well, by doing If this was not done so, this would override any attributes of the contract's state beforehand. This is why the conditional here is necessary. Thereby, its utility being to initialize the contract in a robust manner. |
Beta Was this translation helpful? Give feedback.
-
@ManuWeb3 Hey! Your concern is correct, In the video, he has done the way you are saying it should be. But I think in the repo he has followed this approach for the tests. He has created a function for it that turns a public variable |
Beta Was this translation helpful? Give feedback.
@ManuWeb3 Hey! Your concern is correct, In the video, he has done the way you are saying it should be. But I think in the repo he has followed this approach for the tests. He has created a function for it that turns a public variable
s_initialized
totrue
after initializing so he can test it in the unit tests. Because otherwise, the array is internal he could not get it in the tests. Also another approach he can follow is that he can create a getter function for array that returns its length. But these all are approaches that differes from person to person.