2

I want to map an address to two different field. which one is correct or better ?

Approach 1 :

mapping(address => uint256) balances_En;
mapping(address => uint256) balances_PW;

and to return its balance :

return balances_En[_owner]
return balances_PW[_owner]

Approach 2 :

struct Balances {
    uint256 balances_En;
    uint256 balances_PW;
  }
mapping(address => Balances) balances; 

and to return its balance :

return balances[_owner].balances_En
return balances[_owner].balances_PW
3

Both are correct.

After that, it's just a matter of personnal choice. I would go for the second approach, which seems more natural to me. Later, if you want the users to have more informations than just their two balances, it will be easier to simply add it in the struct than do new mappings.

  • 1
    I agree with this, second one would also be my preference while both work. Not only that but I think you'll also save on gas in the end with using the second method compared to the first one. – Gabe Apr 26 '18 at 12:30
  • 1
    Yes, indeed you do, since you only hash the key once. – Eli Drion Apr 26 '18 at 12:48

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.