26

Is owner > 0x0 a good way to test if an address owner was set in Solidity?
I am not sure if this could break something in the future and if there are better ways to see if the address was set. What is the best solution?

45

Though this works, I personally think it is a good idea to have both operand types equal on boolean operations , as you mentioned for future compatibility.

Instead of using > operator use equality == or != , as in case of address the greater operator may not make sense.

Instead of using 0x0 use address(0) or address(0x0), as it would make much more of type of address.

So your code would look like

owner==address(0);// check if the address is not set

Just my 2 cents

  • thanks, what is a Neo Noir Dev? Has it to do with Film Noir? – Roland Kofler Jul 2 '16 at 20:14
  • 8
    Is there documentation for this address(0) somewhere? – JohnAllen Aug 20 '17 at 18:18
  • 1
    it merely typecasts uint 0 to an address (0x000...) – Nico Vergauwen Jun 18 '18 at 14:01
  • @JohnAllen as of Nov 2018, it doesn't seem so. – Paul Razvan Berg Nov 13 '18 at 8:31
  • @JohnAllen it is not documented per se in the Solidity documentation, but both contracts Voting and BlindAuction mention them : solidity.readthedocs.io/en/v0.5.11/… – CJ42 Aug 15 at 9:38

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.