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

后端 未结 9 889
故里飘歌
故里飘歌 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:59

    IMHO, you should combine items into logical groupings and create your files based on that.

    When I'm writing functions, there are often a half a dozen or so that are tightly related to each other. I tend to put them together in a single header and implementation file.

    When I write classes, I usually limit myself to one heavyweight class per header and implementation file. I might add in some convenience functions or tiny helper classes.

    If I find that an implementation file is thousands of lines long, that's usually a sign that there's too much there and I need to break it up.

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

    We use the principle of one external function per file. However, within this file there may be several other "helper" functions in unnamed namespaces that are used to implement that function.

    In our experience, contrary to some other comments, this has had two main benefits. The first is build times are faster as modules only need to be rebuilt when their specific APIs are modified. The second advantage is that by using a common naming scheme, it is never necessary to spend time searching for the header that contains the function you wish to call:

    // getShapeColor.h
    Color getShapeColor(Shape);
    
    // getTextColor.h
    Color getTextColor(Text);
    

    I disagree that the standard library is a good example for not using one (external) function per file. Standard libraries never change and have well defined interfaces and so neither of the points above apply to them.

    That being said, even in the case of the standard library there are some potential benefits in splitting out the individual functions. The first is that compilers could generate a helpful warning when unsafe versions of functions are used, e.g. strcpy vs. strncpy, in a similar way to how g++ used to warn for inclusion of <iostream.h> vs. <iostream>.

    Another advantage is that I would no longer be caught out by including memory when I want to use memmove!

    0 讨论(0)
  • 2021-01-02 04:00

    You can redeclare some of your functions as being static methods of one or more classes: this gives you an opportunity (and a good excuse) to group several of them into a single source file.

    One good reason for having or not having several functions in one source files, if that source file are one-to-one with object files, and the linker links entire object files: if an executable might want one function but not another, then put them in separate source files (so that the linker can link one without the other).

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