I’ve mostly been putting functions, structs enums and tests in the same file and it’s starting to feel like a mess… I am constantly losing track of which function is where and I’m never quite sure if I have code in the right place. I know this is kind of vague, but that’s been my general feeling while working in my largest personal project so far. It’s essentially a large Algorithm that has many smaller steps that needs to run in a sequence.

I think I might just have too many loose functions and maybe I should use more impl methods and traits? I’m also thinking I should try the builder pattern in a few spots.

Anyone have any guidance on code organization in rust?

  • steventrouble@programming.dev
    link
    fedilink
    arrow-up
    7
    ·
    1 year ago

    At Google, my team had a tried and true policy on organization:

    "It doesn’t matter how you organize the code just do it consistently.

    Don’t sweat the specifics too much, just try to be consistent in how you name and order your functions. That’s the magic ingredient that lets you go from one part of the code to another without having to ramp up.

    Specifically regarding traits and impls and such, they can be good, but I wouldn’t worry if you’re not using them. It’s good to experiment with new paradigms, but you don’t have to. Many successful programs and libraries are written using nothing but functions.

    If you’re interested in organization, I strongly recommend learning about code smells and refactorings (e.g. refactoring.guru). Also unit testing can lead you toward better organization because it forces you to think about how you’ll call the code. Besides that, just have fun!

    • nerdblood@programming.devOP
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

      This is really good to hear, I don’t think I’m as far off base as I thought; maybe I’ve been over thinking it a bit. And thanks for that refactoring resource. I’m very big into making my TS code clean and maintainable. I’m just thrown off a bit with the new paradigm.