<p>For whatever reason, our company has a coding guideline that states:</p>

<p><code>Each class shall have it's own header and implementation file.</code></p>

<p>So if we wrote a class called <code>MyString</code> we would need an associated <strong>MyStringh.h</strong> and <strong>MyString.cxx</strong>.</p>

<p>Does anyone else do this?  Has anyone seen any compiling performance repercussions as a result?  Does 5000 classes in 10000 files compile just as quickly as 5000 classes in 2500 files?  If not, is the difference noticeable?</p>

<p>[We code C++ and use GCC 3.4.4 as our everyday compiler]</p>

[{'answer_id': 28173, 'author': 'Magnus Westin', 'author_id': 2957, 'author_profile': 'https://Stackoverflow.com/users/2957', 'pm_score': 4, 'selected': False, 'text': "<p>We do that at work, its just easier to find stuff if the class and files have the same name. As for performance, you really shouldn't have 5000 classes in a single project. If you do, some refactoring might be in order.</p>\n\n<p>That said, there are instances when we have multiple classes in one file. And that is when it's just a private helper class for the main class of the file.</p>\n"}, {'answer_id': 28181, 'author': 'kokos', 'author_id': 1065, 'author_profile': 'https://Stackoverflow.com/users/1065', 'pm_score': 2, 'selected': False, 'text': "<p>It's common practice to do this, especially to be able to include .h in the files that need it. Of course the performance is affected but try not to think about this problem until it arises :).<br>\nIt's better to start with the files separated and after that try to merge the .h's that are commonly used together to improve performance if you really need to. It all comes down to dependencies between files and this is very specific to each project.</p>\n"}, {'answer_id': 28194, 'author': 'Chris Marasti-Georg', 'author_id': 96, 'author_profile': 'https://Stackoverflow.com/users/96', 'pm_score': 4, 'selected': False, 'text': "<p>+1 for separation.  I just came onto a project where some classes are in files with a different name, or lumped in with another class, and it is impossible to find these in a quick and efficient manner.  You can throw more resources at a build - you can't make up lost programmer time because (s)he can't find the right file to edit.</p>\n"}, {'answer_id': 28198, 'author': 'Huppie', 'author_id': 1830, 'author_profile': 'https://Stackoverflow.com/users/1830', 'pm_score': 1, 'selected': False, 'text': "<p>The same rule applies here, but it notes a few exceptions where it is allowed Like so:</p>\n\n<ul>\n<li>Inheritance trees</li>\n<li>Classes that are only used within a <strong>very</strong> limited scope</li>\n<li>Some Utilities are simply placed in a general 'utils.h'</li>\n</ul>\n"}, {'answer_id': 28204, 'author': 'Rob Wells', 'author_id': 2974, 'author_profile': 'https://Stackoverflow.com/users/2974', 'pm_score': 3, 'selected': False, 'text': "<p>Most places where I have worked have followed this practice. I've actually written coding standards for BAE (Aust.) along with the reasons why instead of just carving something in stone with no real justification.</p>\n<p>Concerning your question about source files, it's not so much time to compile but more an issue of being able to find the relevant code snippet in the first place. Not everyone is using an IDE. And knowing that you just look for MyClass.h and MyClass.cpp really saves time compared to running &quot;grep MyClass *.(h|cpp)&quot; over a bunch of files and then filtering out the #include MyClass.h statements...</p>\n<p>Mind you there are work-arounds for the impact of large numbers of source files on compile times. See Large Scale C++ Software Design by John Lakos for an interesting discussion.</p>\n<p>You might also like to read Code Complete by Steve McConnell for an excellent chapter on coding guidelines. Actualy, this book is a great read that I keep coming back to regularly.</p>\n<p>N.B. You need the first edition of Code Complete that is easily available online for a copy. The interesting section on coding and naming guidelines didn't make it into Code Complete 2.</p>\n"}, {'answer_id': 28215, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 8, 'selected': True, 'text': '<p>The term here is <strong>translation unit</strong> and you really want to (if possible) have one class per translation unit ie, one class implementation per .cpp file, with a corresponding .h file of the same name.</p>\n\n<p>It\'s usually more efficient (from a compile/link) standpoint to do things this way, especially if you\'re doing things like incremental link and so forth. The idea being, translation units are isolated such that, when one translation unit changes, you don\'t have to rebuild a lot of stuff, as you would have to if you started lumping many abstractions into a single translation unit.</p>\n\n<p>Also you\'ll find many errors/diagnostics are reported via file name ("Error in Myclass.cpp, line 22") and it helps if there\'s a one-to-one correspondence between files and classes. (Or I suppose you could call it a 2 to 1 correspondence).</p>\n'}, {'answer_id': 28216, 'author': 'Matt Dillard', 'author_id': 863, 'author_profile': 'https://Stackoverflow.com/users/863', 'pm_score': 3, 'selected': False, 'text': '<p>In addition to simply being "clearer", separating classes into separate files makes it easier for multiple developers not to step on each others toes.  There will be less merging when it comes time to commit changes to your version control tool.</p>\n'}, {'answer_id': 30604, 'author': 'Brian Stewart', 'author_id': 3114, 'author_profile': 'https://Stackoverflow.com/users/3114', 'pm_score': 2, 'selected': False, 'text': '<p>The best practice, as others have said, is to place each class in its own translation unit from a code maintenance and understandability perspective. However on large scale systems this is sometimes not advisable - see the section entitled "Make Those Source Files Bigger" in <a href="http://www.cygnus-software.com/papers/precompiledheaders.html" rel="nofollow noreferrer">this article</a> by Bruce Dawson for a discussion of the tradeoffs.</p>\n'}, {'answer_id': 90211, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>It is very helpful to have only have one class per file, but if you do your building via bulkbuild files which include all the individual C++ files, it makes for faster compilations since startup time is relatively large for many compilers.</p>\n'}, {'answer_id': 105329, 'author': 'paercebal', 'author_id': 14089, 'author_profile': 'https://Stackoverflow.com/users/14089', 'pm_score': 6, 'selected': False, 'text': '<h2>Overwhelmed by thousands lines of code?</h2>\n<p>Having one set of header/source files per class in a directory can seem overkill. And if the number of classes goes toward 100 or 1000, it can even be frightening.</p>\n<p>But having played with sources following the philosophy &quot;let\'s put together everything&quot;, the conclusion is that only the one who wrote the file has any hope to not be lost inside. Even with an IDE, it is easy to miss things because <strong>when you\'re playing with a source of 20,000 lines, you just close your mind for anything not exactly referring to your problem.</strong></p>\n<p><i>Real life example: the class hierarchy defined in those thousand lines sources closed itself into a diamond-inheritance, and some methods were overridden in child classes by methods with exactly the same code. This was easily overlooked (who wants to explore/check a 20,000 lines source code?), and when the original method was changed (bug correction), the effect was not as universal as excepted.</i></p>\n<h2>Dependancies becoming circular?</h2>\n<p>I had this problem with templated code, but I saw similar problems with regular C++ and C code.</p>\n<p>Breaking down your sources into 1 header per struct/class lets you:</p>\n<ul>\n<li>Speed up compilation because you can use symbol forward-declaration instead of including whole objects</li>\n<li>Have circular dependencies between classes (§) (i.e. class A has a pointer to B, and B has a pointer to A)</li>\n</ul>\n<p>In source-controlled code, class dependencies could lead to regular moving of classes up and down the file, just to make the header compile. You don\'t want to study the evolution of such moves when comparing the same file in different versions.</p>\n<p><strong>Having separate headers makes the code more modular, faster to compile, and makes it easier to study its evolution through different versions diffs</strong></p>\n<p><i>For my template program, I had to divide my headers into two files: The .HPP file containing the template class declaration/definition, and the .INL file containing the definitions of the said class methods.</p>\n<p>Putting all this code inside one and only one unique header would mean putting class definitions at the beginning of this file, and the method definitions at the end.</p>\n<p>And then, if someone needed only a small part of the code, with the one-header-only solution, they still would have to pay for the slower compilation.</i></p>\n<p>(§) Note that you can have circular dependencies between classes if you know which class owns which. This is a discussion about classes having knowledge of the existence of other classes, not shared_ptr circular dependencies antipattern.</p>\n<h2>One last word: Headers should be self-sufficients</h2>\n<p>One thing, though, that must be respected by a solution of multiple headers and multiple sources.</p>\n<p><strong>When you include one header, no matter which header, your source must compile cleanly.</strong></p>\n<p>Each header should be self-sufficient. You\'re supposed to develop code, not treasure-hunting by greping your 10,000+ source files project to find which header defines the symbol in the 1,000 lines header you need to include just because of <em>one</em> enum.</p>\n<p>This means that either each header defines or forward-declare all the symbols it uses, or include all the needed headers (and only the needed headers).</p>\n<h2>Question about circular dependencies</h2>\n<p><a href="https://stackoverflow.com/users/2757035/underscore-d">underscore-d</a> asks:</p>\n<blockquote>\n<p>Can you explain how using separate headers makes any difference to circular dependencies? I don\'t think it does. We can trivially create a circular dependency even if both classes are fully declared in the same header, simply by forward-declaring one in advance before we declare a handle to it in the other. Everything else seems to be great points, but the idea that separate headers facilitate circular dependencies seems way off</p>\n<p>underscore_d, Nov 13 at 23:20</p>\n</blockquote>\n<p>Let\'s say you have 2 class templates, A and B.</p>\n<p>Let\'s say the definition of class A (resp. B) has a pointer to B (resp. A). Let\'s also say the methods of class A (resp. B) actually call methods from B (resp. A).</p>\n<p>You have a circular dependency both in the definition of the classes, and the implementations of their methods.</p>\n<p>If A and B were normal classes, and A and B\'s methods were in .CPP files, there would be no problem: You would use a forward declaration, have a header for each class definitions, then each CPP would include both HPP.</p>\n<p>But as you have templates, you actually have to reproduce that patterns above, but with headers only.</p>\n<p>This means:</p>\n<ol>\n<li>a definition header <code>A.def.hpp</code> and <code>B.def.hpp</code></li>\n<li>an implementation header <code>A.inl.hpp</code> and <code>B.inl.hpp</code></li>\n<li>for convenience, a &quot;naive&quot; header <code>A.hpp</code> and <code>B.hpp</code></li>\n</ol>\n<p>Each header will have the following traits:</p>\n<ol>\n<li>In <code>A.def.hpp</code> (resp. <code>B.def.hpp</code>), you have a forward declaration of class B (resp. A), which will enable you to declare a pointer/reference to that class</li>\n<li><code>A.inl.hpp</code> (resp. <code>B.inl.hpp</code>) will include both <code>A.def.hpp</code> and <code>B.def.hpp</code>, which will enable methods from A (resp. B) to use the class B (resp. A).</li>\n<li><code>A.hpp</code> (resp. <code>B.hpp</code>) will directly include both <code>A.def.hpp</code> and <code>A.inl.hpp</code> (resp. <code>B.def.hpp</code> and <code>B.inl.hpp</code>)</li>\n<li>Of course, all headers need to be self sufficient, and protected by header guards</li>\n</ol>\n<p>The naive user will include <code>A.hpp</code> and/or <code>B.hpp</code>, thus ignoring the whole mess.</p>\n<p>And having that organization means the library writer can solve the circular dependencies between A and B while keeping both classes in separate files, easy to navigate once you understand the scheme.</p>\n<p>Please note that it was an edge case (two templates knowing each other). I expect most code to <em>not</em> need that trick.</p>\n'}, {'answer_id': 4329805, 'author': 'Francois', 'author_id': 400925, 'author_profile': 'https://Stackoverflow.com/users/400925', 'pm_score': 1, 'selected': False, 'text': '<p>I found these guidelines particularly useful when it comes to header files :\n<a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Header_Files" rel="nofollow">http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Header_Files</a></p>\n'}, {'answer_id': 27821735, 'author': 'Peter Ritter', 'author_id': 3666846, 'author_profile': 'https://Stackoverflow.com/users/3666846', 'pm_score': 0, 'selected': False, 'text': "<p>I'm surprised that almost everyone is in favor of having one file per class. The problem with that is that in the age of 'refactoring' one may have a hard time keeping the file and class names in synch. Everytime you change a class name, you then have to change the file name too, which means that you have to also make a change everywhere the file is included. </p>\n\n<p>I personally group related classes into a single files and then give such a file a meaningful name that won't have to change even if a class name changes. Having fewer files also makes scrolling through a file tree easier. \nI use Visual Studio on Windows and Eclipse CDT on Linux, and both have shortcut keys that take you straight to a class declaration, so finding a class declaration is easy and quick. </p>\n\n<p>Having said that, I think once a project is completed, or its structure has 'solidified', and name changes become rare, it may make sense to have one class per file. I wish there was a tool that could extract classes and place them in distinct .h and .cpp files. But I don't see this as essential.</p>\n\n<p>The choice also depends on the type of project one works on. In my opinion the issue doesn't deserve a black and white answer since either choice has pros and cons. </p>\n"}, {'answer_id': 55319843, 'author': 'nul operator', 'author_id': 10217208, 'author_profile': 'https://Stackoverflow.com/users/10217208', 'pm_score': 1, 'selected': False, 'text': "<p>Two words: Ockham's Razor. Keep one class per file with the corresponding header in a separate file. If you do otherwise, like keeping a piece of functionality per file, then you have to create all kinds of rules about what constitutes a piece of functionality. There is much more to gain by keeping one class per file. And, even half decent tools can handle large quantities of files. Keep it simple my friend.</p>\n"}]
['https://Stackoverflow.com/questions/28160', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1881/']