Search code examples
code-organization

How to effectively organize code in languages where the implementation and interface declaration are typically in the same file


I usally code in C and C++ where implementation and declaration are in different files (.c/.h and .cpp/.hpp), but I often code in Haskell/Python/D where this distinction does not exist.

My problem is when my code tends to grow I struggle to have a clear vision of what is inside a file. I miss the "you know what to expect just by looking at the .h" and tend to become overwhelmed by the feeling of mess.

My best attempt to solve this is to put fold into the file, but I would like to know how do you do guys? Do you have some magic solutions that I haven't tried yet? Is that just a set of mind?


Solution

  • I don't think there are magic solutions, but the following tips might work

    • Use classes
    • Describe for each class their resonsibility
      • Write down which data is used in each class
      • Write down which functionality is used in each class
    • Start with small classes, they will grow eventually
    • When classes getting too big, split them.
    • Use one file per class.
    • Split methods/data in public/private (with the use of the convention _)