0

I have seen two ways to assign the creator of the contract. The first way is this way which you don't put the assignment in the constructor function.

pragma solidity ^0.6.6;

contract Test {
   address creator = msg.sender;

   constructor() public {}
}

The second way is when you assign the creator inside the constructor function

pragma solidity ^0.6.6;

contract Test {

   constructor() public {
       address creator = msg.sender;
   }
}

Is there any difference between these two? which one is better? and why?

EDIT : i wrote the second code in a totally wrong way. i meant this code

pragma solidity ^0.6.6;

contract Test {
   address creator;

   constructor() public{
       creator = msg.sender;
   }
}
1

You have to store it in a state variable, otherwise the data is only available during the same transaction and in the same function. Your second version doesn't store the value in state.

Therefore the first version is the only viable way. Of course, if you want, you can give the creator as a parameter to the constructor, assign the value to a state variable inside the constructor and therefore have a different creator for the contract.

Edit after edit to the original post

Now that the second version has been edited to be a reasonable one I must say that I can't think of any reasonable difference in them. I tried them in Remix and both have exactly the same gas deployment cost and they both work the same way. There is probably a slight difference in when the actual value gets populated but I can't think of any way to (ab)use that difference. So both versions are equally good - just a matter of taste.

2
  • OMG i realized i wrote a wrong code for the second way. Thank you. i edited it now. I will be thankful if you check it out again. – Masoud jt May 14 '20 at 13:40
  • Ok, checked and edited answer – Lauri Peltonen May 14 '20 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.