Encode-Club-Solidity-Foundations / Lesson-02

4 stars 7 forks source link

Why Structs? #16

Open Ashleybrandon opened 1 year ago

Ashleybrandon commented 1 year ago

Why do structs exist in Solidity? Trying to understand their purpose beyond classes/objects. Thanks so much.

MatheusDaros commented 1 year ago

Hello @Ashleybrandon

Thanks for your question.

Structs are great resources for both organization and optimization.

For example, lets pick the Ballot contract. If we couldn't struct data in solidity, we would have to keep and manage several mappings to keep each attribute of a voter, and also several arrays to keep every attribute of each proposal.

This wouldn't only be more costly, but also more prone to error to manage, not to mention the organization and amount of code to do the same thing without structs.