uint8_t + enum For Statuses
This one is more of a little tip/trick that I like to use - it's not necessarily something you need to know in order to write smart contracts.
There are plenty of times where you'll need to store some type of status in a table, or struct. True/false
, open/closed
, etc. Since you can't modify table structures without going through a complicated process, I've learned over the years that it's best to always plan in advance, and allow for the possibility that you might need more than 2 options.
For this reason, I use uint8_t
to store these values, instead of a boolean. Then I combine this with an enum
in order to improve code clarity / readability.
Let's pretend for example that you have some actions on your contract, and you only want specific users to be able to execute these actions. So, you put a flag in your table called is_authorized
, like this.
The problem you will eventually run into, is that you might add some new action to your contract, which requires higher authorization than a regular user, but less authorization than the users you originally created this flag for. Well, now you have no way of adding a 3rd option.
This is the reason that I use uint8_t
instead.
With this approach, your contract remains flexible and is able to adapt to any new features you may add in the future. If you were simply using a bool
, you would've had to create a new table in order to extend the current privilege structure.
There is a problem here, though. When other parts of your contract are comparing against uint8_t
to determine a status, it becomes difficult to keep track of what each number is supposed to mean. This is where enums
come into play.
This allows you to use uint8_t
for storage, but also allows you to keep the code clean and readable, which is always important (for yourself and for other developers).
Last updated