Header per source file

后端 未结 7 1653
醉梦人生
醉梦人生 2020-11-29 05:53

I\'m trying to understand the purpose behind one header per each source file method. As I see it, headers are meant for sharing function declarations, typedef\'

相关标签:
7条回答
  • 2020-11-29 06:18

    Logical, structured organisation and small source files enable:

    • faster, better programming - breaking the code into more manageable and understandable chunks makes it easier to find, understand and edit the relevant code.
    • code re-usability - different "modules" of code can be separated into groups of source/header files that you can more easily integrate into different programs.
    • better "encapsulation" - only the .c files that specifically include that header can use the features from it, which helps you to minimise the relationships between different parts of your code, which aids modularity. It doesn't stop you using things from anywhere, but it helps you to think about why a particular c file needs to access functions declared in a particular header.
    • Aids teamwork - two programmers trying to change the same code file concurrently usually cause problems (e.g. exclusive locks) or extra work (e.g. code merges) that slow each other down.
    • faster compiles - if you have one header then every time you make a change in it you must recompile everything. With many small headers, only the .c files that #include the changed header must be rebuilt.
    • easier maintainability & refactoring - for all the above reasons

    In particular, "one header for each source file" makes it very easy to find the declarations relevant to the c file you are working in. As soon as you start to coalesce multiple headers into a single file, it starts to become difficult to relate the c and h files, and ultimately makes building a large application much more difficult. If you're only working on a small application then it's still a good idea to get into the habit of using a scalable approach.

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