Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove initialized fields from as many structs as possible #551

Open
XavierChanth opened this issue Feb 5, 2025 · 0 comments
Open
Assignees

Comments

@XavierChanth
Copy link
Member

Initialized fields was a paradigm introduced to metadata as a way to ignore unset values in metadata so that we could generate minimal metadata protocol strings.

This paradigm has been widely adopted across the codebase, mostly done in #337. It was a massive PR, so we were focused on other things and paid less attention to the paradigms used.

The problems with introducing this paradigm across the SDK:

  • You are forced to use the setters defined by the library or must set the initialized fields bits yourself
    • This means the optimal way to use these structs becomes object oriented, rather than data oriented or procedural. Which also contributes to some of the later problems
  • Struct members which should not have unset values are now unset by default.
  • Initializing or setting values in a struct directly has side-effects and can't be done safely.
    • Passing modifiable pointers to struct members around also has side-effects for the same reasons.
  • More code to write in order to use the library, more code to maintain
    • An example of this is doing a notify:update which was previously ~5 function calls is now ~20
      • And most of these ~20 function calls can produce errors, which means you also have to write error handling an extra ~12 times.
  • We require extra private functions, which means separating them from the public interface, further complicating code maintenance. This also means more private header files if we want to test, introduce .c files which have both public & private headers associated with them.

The solution for most of these things is to have sensible unset values for optional struct members:

For pointers, we have NULL to represent this.
For numerical values: often space in the negative range or 0 for ports.
Booleans: are actually a byte so they have space for unset value
Packed booleans: use 2 bits, one for initialized, one for value

@XavierChanth XavierChanth self-assigned this Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant