-
Notifications
You must be signed in to change notification settings - Fork 39
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
refactor(core,accountsdb,bincode,net): Fixes, improvements, renames + discuss #315
Conversation
It was always already just 32 bytes, there was no real reason for it to be dynamically allocated.
* fix `bincode.free` for various types * renames & unifies `arraylist.defaultArrayList[Unmanaged]OnEOFConfig` to `arraylist.defaultOnEofConfig`. * renames `arraylist.arrayListFieldConfig` to `arraylist.standardConfig`. * renames `shortvec.ShortVecConfig` to `shortvec.sliceConfig`. * renames `shortvec.ShortVecArrayListConfig` to `shortvec.arrayListConfig`.
a2ea8d5
to
f414cf4
Compare
I think this was just an oversight. My understanding is that it was a deliberate style choice. To conform to the convention, I've been trying to use uppercase for file-scoped consts, unless they are imports. But I think I typically use lowercase for struct-scoped consts. I understand that files are structs but you know what I mean. I don't have a very strong opinion about it personally. If I had to choose, my vote would probably be to use lowercase for all constants.
If all the function does is return a const, then I agree with removing the function. But I think we should distinguish between two situations:
This also works with init functions right? |
I don't have a strong opinion either, but I do like the current "screaming snake case", it's pretty easy to visually see which identifiers are constants. That's just my preference, I do not mind other solutions.
I agree that could be moved to using default field values. For example, an init function that takes no parameters and doesn't do any side-effect like computations inside of itself (incrementing some external counter or something), also doesn't do any extensive pure computation to initialize values (compute some key?), is superfluous.
In agreement!
Yep, const S = struct {
... some fields
fn init(x: u32) S {
... returns S
}
};
const x: S = .init(10); is a valid usage of decl literals. To add to that, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tbh would prefer screaming-case for all constants (agreed const decls are better than an empty method call) - i think it shows the differentiation very clearly and is easy to read
If there is clearly one most sensible and obvious default approach to initialize the struct, then we probably don't even need a decl. Can't we just set defaults for the fields?
i feel like this is more dangerous, because it might allow you to initialize some fields, forget some others and everything compile fine -- with the decls, youre explicit that you want all fields as the default which is less error prone and more clear what you are doing
const x: S = .init(10);
idk about you guys, but thats so hard to read relative to x = S.init(10)
I would say that's more a matter of familiarity, but the former provides strictly more information to the reader, in the least verbose way:
It also comes with the benefit that it unifies all initialization (much nicer in the case of There's also the point that,
With all that considered, I think it would be along the path of least resistance to embrace this style. |
huh never considered the safety guarantees - thats kinda nice - maybe it is just a case of familiarity, i guess time will see |
100%. In many cases setting defaults for fields is subtly a really bad idea, because people will feel safe partially-initialising structs in invalid ways which will cause big problems later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
few spots missed
So would you advocate completely against using defaults for fields in any situation? |
For my part, I would heed the recommendation of the zig documentation itself:
Though besides that, there's also just the point of what it means for a value to be a good "default". I like using specifically named initialization values/procedures for types which are purely data, whose data the programmer will have to directly interface with in some way (as is the case of Pubkey, Hash, Signature, and a myriad of other types). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM other than these could of nits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a couple things I'd prefer, style-wise:
- avoiding SCREAMING_CASE
- .isZeroed() feels a bit better than .isAllZeroes()
But these are really just nitpicks and this PR improves things overall so I'm happy with it.
This PR is mainly comprised of refactors, but also contains some slight deviations from a specific naming convention we follow implicitly: blanket screaming snake case constants.
Note: by implicitly, I mean that it isn't recommended by the zig style guide, nor do we outline it in our style guide.
In this PR I propose that we move to replace superfluous init methods with "init constants", which we could then also take advantage of when upgrading to zig 0.14 with the advent of Decl Literals. This was also in part prompted by a discussion we had on the logger PR. And of course, I am also proposing that we make these init constants normal snake case, instead of screaming snake case.
Also, after we decide on this, we should outline this in our style guide - I can also add that to this change set.
Of course review of any of the other changes in this PR is welcome.