Should I put many functions into one file? Or, more or less, one function per file?

后端 未结 9 937
故里飘歌
故里飘歌 2021-01-02 02:54

I love to organize my code, so ideally I want one class per file or, when I have non-member functions, one function per file.

The reasons are:

  1. When

9条回答
  •  遥遥无期
    2021-01-02 03:40

    I can see some advantages to your approach, but there are several disadvantages:

    1. Including a package is nightmare. You can end up with 10-20 includes to get the functions you need. For example, imagine if STDIO or StdLib was implemented this way.

    2. Browsing the code will be a bit of pain, since in general it is easier to scroll through a file than to switch files. Obviously too big of file is hard, but even there with modern IDEs it is pretty easy to collapse the file down to what you need and a lot of them have function short cut lists.

    3. Make file maintenance is a pain.

    4. I am a huge fan of small functions and refactoring. When you add overhead (making a new file, adding it to source control,...) it encourages people to write longer functions where instead of breaking one function into three parts, you just make one big one.

提交回复
热议问题