<p>Should the folders in a solution match the namespace?</p>

<p>In one of my teams projects, we have a class library that has many sub-folders in the project.</p>

<p>Project Name and Namespace: <code>MyCompany.Project.Section</code>.</p>

<p>Within this project, there are several folders that match the namespace section:</p>

<ul>
<li>Folder <code>Vehicles</code> has classes in the <code>MyCompany.Project.Section.Vehicles</code> namespace</li>
<li>Folder <code>Clothing</code> has classes in the<code>MyCompany.Project.Section.Clothing</code> namespace</li>
<li>etc.</li>
</ul>

<p>Inside this same project, is another rogue folder</p>

<ul>
<li>Folder <code>BusinessObjects</code> has classes in the <code>MyCompany.Project.Section</code> namespace</li>
</ul>

<p>There are a few cases like this where folders are made for "organizational convenience".</p>

<p>My question is: What's the standard?  In class libraries do the folders usually match the namespace structure or is it a mixed bag?</p>

[{'answer_id': 4666, 'author': 'Dan', 'author_id': 230, 'author_profile': 'https://Stackoverflow.com/users/230', 'pm_score': 3, 'selected': False, 'text': '<p>Yes they should, only leads to confusion otherwise.</p>\n'}, {'answer_id': 4669, 'author': 'Ishmaeel', 'author_id': 227, 'author_profile': 'https://Stackoverflow.com/users/227', 'pm_score': 3, 'selected': False, 'text': "<p>I'd say yes.</p>\n\n<p>First, it will be easier to find the actual code files by following down the namespaces (say, when somebody e-mails you a naked exception call stack). If you let your folders go out of sync with namespaces, finding files in big codebases becomes getting tiring.</p>\n\n<p>Second, VS will generate new classes you create in folders with the same namespace of its parent folder structure. If you decide to swim against this, it will be just one more plumbing job to do daily when adding new files.</p>\n\n<p>Of course, this goes without saying that one should be conservative about how deep xis folder/namespace hierarchy goes.</p>\n"}, {'answer_id': 4671, 'author': 'Karl Seguin', 'author_id': 34, 'author_profile': 'https://Stackoverflow.com/users/34', 'pm_score': 5, 'selected': False, 'text': "<p>I think the standard, within .NET, is to try to do it when possible, but not to create unnecessarily deep structures just to adhere to it as a hard rule. None of my projects follow the namespace == structure rule 100% of the time, sometimes its just cleaner/better to break out from such rules.</p>\n\n<p>In Java you don't have a choice. I'd call that a classic case of what works in theory vs what works in practice.</p>\n"}, {'answer_id': 4672, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 7, 'selected': True, 'text': '<p>Also, note that if you use the built-in templates to add classes to a folder, it will by default be put in a namespace that reflects the folder hierarchy.</p>\n\n<p>The classes will be easier to find and that alone should be reasons good enough.</p>\n\n<p>The rules we follow are:</p>\n\n<ul>\n<li>Project/assembly name is the same as the root namespace, except for the .dll ending</li>\n<li>Only exception to the above rule is a project with a .Core ending, the .Core is stripped off</li>\n<li>Folders equals namespaces</li>\n<li>One type per file (class, struct, enum, delegate, etc.) makes it easy to find the right file</li>\n</ul>\n'}, {'answer_id': 55781, 'author': 'Jay Bazuzi', 'author_id': 5314, 'author_profile': 'https://Stackoverflow.com/users/5314', 'pm_score': 5, 'selected': False, 'text': '<p>@lassevk: I agree with these rules, and have one more to add.</p>\n\n<p>When I have nested classes, I still split them out, one per file.  Like this:</p>\n\n<pre><code>// ----- Foo.cs\npartial class Foo\n{\n    // Foo implementation here\n}\n</code></pre>\n\n<p>and</p>\n\n<pre><code>// ----- Foo.Bar.cs\npartial class Foo\n{\n    class Bar\n    {\n        // Foo.Bar implementation here\n    }\n}\n</code></pre>\n'}, {'answer_id': 29255554, 'author': 'Weyland Yutani', 'author_id': 2086856, 'author_profile': 'https://Stackoverflow.com/users/2086856', 'pm_score': 6, 'selected': False, 'text': '<p>No.</p>\n\n<p>I\'ve tried both methods on small and large projects, both with single (me) and a team of developers.</p>\n\n<p>I found the simplest and most productive route was to have a single namespace per project and all classes go into that namespace. You are then free to put the class files into whatever project folders you want. There is no messing about adding using statements at the top of files all the time as there is just a single namespace.</p>\n\n<p>It is important to organize source files into folders and in my opinion that\'s all folders should be used for. Requiring that these folders also map to namespaces is unnecessary, creates more work, and I found was actually harmful to organization because the added burden encourages disorganization.</p>\n\n<p>Take this FxCop warning for example:</p>\n\n<blockquote>\n  <p><strong>CA1020: Avoid namespaces with few types</strong><br>\n  cause: A namespace other than the global namespace contains fewer than five types\n  <a href="https://msdn.microsoft.com/en-gb/library/ms182130.aspx">https://msdn.microsoft.com/en-gb/library/ms182130.aspx</a></p>\n</blockquote>\n\n<p>This warning encourages the dumping of new files into a generic Project.General folder, or even the project root until you have four similar classes to justify creating a new folder. Will that ever happen?</p>\n\n<p><strong>Finding Files</strong></p>\n\n<p>The accepted answer says "The classes will be easier to find and that alone should be reasons good enough."</p>\n\n<p>I suspect the answer is referring to having multiple namespaces in a project which don\'t map to the folder structure, rather than what I am suggesting which is a project with a single namespace.</p>\n\n<p>In any case while you can\'t determine which folder a class file is in from the namespace, you can find it by using Go To Definition or the search solution explorer box in Visual Studio. Also this isn\'t really a big issue in my opinion. I don\'t expend even 0.1% of my development time on the problem of finding files to justify optimizing it.</p>\n\n<p><strong>Name clashes</strong></p>\n\n<p>Sure creating multiple namespaces allows project to have two classes with the same name. But is that really a good thing? Is it perhaps easier to just disallow that from being possible? Allowing two classes with the same name creates a more complex situation where 90% of the time things work a certain way and then suddenly you find you have a special case. Say you have two Rectangle classes defined in separate namespaces:</p>\n\n<ul>\n<li>class Project1.Image.Rectangle </li>\n<li>class Project1.Window.Rectangle</li>\n</ul>\n\n<p>It\'s possible to hit an issue that a source file needs to include both namespaces. Now you have to write out the full namespace everywhere in that file:</p>\n\n<pre><code>var rectangle = new Project1.Window.Rectangle();\n</code></pre>\n\n<p>Or mess about with some nasty using statement:</p>\n\n<pre><code>using Rectangle = Project1.Window.Rectangle;\n</code></pre>\n\n<p>With a single namespace in your project you are forced to come up with different, and I\'d argue more descriptive, names like this:</p>\n\n<ul>\n<li>class Project1.ImageRectangle </li>\n<li>class Project1.WindowRectangle</li>\n</ul>\n\n<p>And usage is the same everywhere, you don\'t have to deal with a special case when a file uses both types.</p>\n\n<p><strong>using statements</strong>  </p>\n\n<pre><code>using Project1.General;  \nusing Project1.Image;  \nusing Project1.Window;  \nusing Project1.Window.Controls;  \nusing Project1.Shapes;  \nusing Project1.Input;  \nusing Project1.Data;  \n</code></pre>\n\n<p>vs</p>\n\n<pre><code>using Project1;\n</code></pre>\n\n<p>The ease of not having to add namespaces all the time while writing code. It\'s not the time it takes really, it\'s the break in flow of having to do it and just filling up files with lots of using statements - for what? Is it worth it?</p>\n\n<p><strong>Changing project folder structure</strong>  </p>\n\n<p>If folders are mapped to namespaces then the project folder path is effectively hard-coded into each source file. This means any rename or move of a file or folder in the project requires actual file contents to change. Both the namespace declaration of files in that folder and using statements in a whole bunch of other files that reference classes in that folder. While the changes themselves are trivial with tooling, it usually results in a large commit consisting of many files whose classes haven\'t even changed.</p>\n\n<p>With a single namespace in the project you can change project folder structure however you want without any source files themselves being modified.</p>\n\n<p><strong>Visual Studio automatically maps the namespace of a new file to the project folder it\'s created in</strong>  </p>\n\n<p>Unfortunate, but I find the hassle of correcting the namespace is less than the hassle of dealing with them. Also I\'ve got into the habit of copy pasting an existing file rather than using Add->New.</p>\n\n<p><strong>Intellisense and Object Browser</strong> </p>\n\n<p>The biggest benefit in my opinion of using multiple namespaces in large projects is having extra organization when viewing classes in any tooling that displays classes in a namespaces hierarchy. Even documentation. Obviously having just one namespace in the project results in all classes being displayed in a single list rather than broken into categories. However personally I\'ve never been stumped or delayed because of a lack of this so I don\'t find it a big enough benefit to justify multiple namespaces.</p>\n\n<p>Although if I were writing a large public class library then I <em>would</em> probably use multiple namespaces in the project so that the assembly looked neat in the tooling and documentation.</p>\n'}, {'answer_id': 51346046, 'author': 'user1451111', 'author_id': 1451111, 'author_profile': 'https://Stackoverflow.com/users/1451111', 'pm_score': 1, 'selected': False, 'text': "<blockquote>\n  <p>What's the standard?</p>\n</blockquote>\n\n<p>There is no official standard but conventionally the folder-to-namespace mapping pattern is most widely used.</p>\n\n<blockquote>\n  <p>In class libraries do the folders usually match the namespace\n  structure or is it a mixed bag?</p>\n</blockquote>\n\n<p>Yes, in most class libraries the folders match the namespace for organizational ease.</p>\n"}]
['https://Stackoverflow.com/questions/4664', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']