this post was submitted on 13 Apr 2023
8 points (100.0% liked)
Rust Programming
8163 readers
2 users here now
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I would also definitely recommend hierarchical sorting. So, at the top would be a type typically used when calling your API or the aggregate type holding your internal state etc. and then right below each of these, the types they are composed of.
Reading code requires sifting through tons of complexity and it requires not-sifting through irrelevant complexity. So, I want to know that I need a type before I care about its precise definition.
For multiple, not hierarchically related types, I try to put the most important, most frequently used type at the top.
And I usually put types above functions, as functions can entail a lot of code + details, so I don't want them 'displacing' any relevant types.
But yeah, I'm not a fan of hard rules and there's definitely exceptions to these.
In a codebase at $DAYJOB, we often have
Configuration
types, which are just boilerplate to read out part of a config file and make it easily accessible to the code in this module.No one cares how those
Configuration
types look, so we place them below all the functions.And sometimes, you'll have a module with many types and not really a reason to deem one more important than the other. Then just sorting them alphabetically can be more readable.
Makes sense. I've also given a try to the flags I mentioned in rustfmt and they are apparently just for the nightly build, which makes me think they are working on it, which is good news (at least for me).
Sometimes having too much freedom when it comes to organizing code can be worse, I guess. But in general I prefer it to, for example, C, which was so picky with the placements of things.
Thanks for the feedback ๐