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

后端 未结 9 887
故里飘歌
故里飘歌 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.

    0 讨论(0)
  • 2021-01-02 03:46

    One function per file has a technical advantage if you're making a static library (which I guess it's one of the reasons why projects like the Musl-libc project follow this pattern).

    Static libraries are linked with object-file granularity and so if you have a static library libfoobar.a composed of*:

     foo.o
         foo1
         foo2
     bar.o
         bar
    

    then if you link the lib for the bar function, the bar.o archive member will get linked but not the foo.o member. If you link for foo1, then the foo.o member will get linked, bringing in the possibly unnecessary foo2 function.

    There are possibly other ways of preventing unneeded functions from being linked in (-ffunction-sections -fdata-sections and --gc-sections) but one function per file is probably most reliable.


    • I'm ignoring C++ name mangling here for the sake of simplicity
    0 讨论(0)
  • 2021-01-02 03:46

    An old programming professor of mine suggested breaking up modules every several hundred lines of code for maintainability. I don't develop in C++ anymore, but in C# I restrict myself to one class per file, and size of the file doesn't matter as long as there's nothing unrelated to my object. You can make use of #pragma regions to gracefully reduce editor space, not sure if the C++ compiler has them, but if it does then definitely make use of them.

    If I were still programming in C++ I would group functions by usage using multiple functions per file. So I may have a file called 'Service.cpp' with a few functions that define that "service". Having one function per file will in turn cause regret to find its way back into your project somehow, someway.

    Having several thousand lines of code per file isn't necessary some of the time though. Functions themselves should never be much more than a few hundred lines of code at most. Always remember that a function should only do one thing and be kept minimal. If a function does more than one thing, it should be refactored into helper methods.

    It never hurts to have multiple source files that define a single entity either. Ie: 'ServiceConnection.cpp' 'ServiceSettings.cpp', and so on so forth.

    Sometimes if I make a single object, and it owns other objects I will combine multiple classes into a single file. For example a button control that contains 'ButtonLink' objects, I might combine that into the Button class. Sometimes I don't, but that's a "preference of the moment" decision.

    Do what works best for you. Experiment a little with different styles on smaller projects can help. Hope this helps you out a bit.

    0 讨论(0)
  • 2021-01-02 03:56

    I also tried to split files in a function per file, but it had some drawbacks. Sometimes functions tend to get larger than they need to (you don't want to add a new .c file every time) unless you are diligent about refactoring your code (I am not).

    Currently I put one to three functions in a .c file and group all the .c files for a functionality in a directory. For header files I have Funcionality.h and Subfunctionality.h so that I can include all the functions at once when needed or just a small utility function if the whole package is not needed.

    0 讨论(0)
  • 2021-01-02 03:56

    For the header part, you should combine items into logical groupings and create your header files based on that. This seems and is very logical IMHO.

    For the source part, you should put each function implementation in a separate source file (static functions are exceptions in this case). This may not seem logical at first, but remember, a compiler knows about the functions, but a linker knows only about the .o and .obj files and its exported symbols. This may change the size of the output file considerably, and this is a very important issue for embedded systems.

    Checkout glibc or Visual C++ CRT source tree...

    0 讨论(0)
  • 2021-01-02 03:57

    One function per file could get messy in my opinion. Imagine if POSIX and ANSI C headers were made the same way.

    #include <strlen.h>
    #include <strcpy.h>
    #include <strncpy.h>
    #include <strchr.h>
    #include <strstr.h>
    #include <malloc.h>
    #include <calloc.h>
    #include <free.h>
    #include <printf.h>
    #include <fprintf.h>
    #include <vpritnf.h>
    #include <snprintf.h>
    

    One class per file is a good idea though.

    0 讨论(0)
提交回复
热议问题