Discussion: namespace from directory structure #2908
Replies: 31 comments 25 replies
-
I'm not a fan of having code files have to be aware of the file system. It means moving a file around now becomes a breaking change. I write a lot of TypeScript as well as C#, and the fact that imports in TS/JS rely on file paths for correctness is one of the most painful and poorly designed things about the language, IMO. |
Beta Was this translation helpful? Give feedback.
-
I'm kind of with @MgSam on this one. Aside from A feature like this would require a companion feature to establish the root path of the project, as not everyone would want the namespaces hanging off of the root project directory. |
Beta Was this translation helpful? Give feedback.
-
That being said this could easily be a plugin/setting on an IDE rather than a language feature |
Beta Was this translation helpful? Give feedback.
-
I'm torn on this one. I like the fact that R# warns/errors if the namespace doesn't match the file path (VS may do this natively now days; I've not checked). So I like the idea of not even having to specify the namespace, but to have it inferred by the file's location. But I think @MgSam also raises some valid points against the idea. So I've upvoted both the proposal and his counter argument. 😄 |
Beta Was this translation helpful? Give feedback.
-
@HaloFour what, you don't look forward to using libraries like this? 🤣
|
Beta Was this translation helpful? Give feedback.
-
Creating a new .cs file already generates namespace based on directory structure. What you need is an analyzer with code fix to apply |
Beta Was this translation helpful? Give feedback.
-
The request is to do this without explicitly specifying the namespace in the code file at all. The namespace would be derived from the file location only at compile time. Bugs galore... |
Beta Was this translation helpful? Give feedback.
-
I like this idea. An analyzer can warn that the namespace doesn't match the directory structure given a configured source root, and can offer a fix to update the namespace to match the location of the file. This would work similarly to how many IDEs deal with the package names of Java files. |
Beta Was this translation helpful? Give feedback.
-
Some comments are really going overboard with the source root configuration. There's already a <RootNameSpace> configuration in the .csproj and the root can be the project file itself. No need to overcomplicate things. I mean, the new project type already automatically picks up .cs files under its path, why not automatically assign namespaces as well? In my opinion this is how it should be:
Example:
If the file DateUtils.cs doesn't have a namespace{ } declaration, its namespace shall be "MyProject.Utils". Today most people just do this but keep the redundant namespace declaration in every file. |
Beta Was this translation helpful? Give feedback.
-
This would be a breaking change for code that does not specify namespace currently, as the types go into the global namespace. |
Beta Was this translation helpful? Give feedback.
-
Hmm, it's global. I can see now why the OP suggested "namespace;" to trigger the feature. Still think my suggestion should be considered for the next time .csproj gets breaking changes. Whenever it may be. ;) |
Beta Was this translation helpful? Give feedback.
-
I would love to see this. I'm not sure if it is possible, but it would be nice if the namespace could be an optional statement. If no namespace is provided, the compiler will determine the namespace based on directory structure. This would (hopefully) solve the issue of backwards compatibility. |
Beta Was this translation helpful? Give feedback.
-
@jingle1000 as just noted namespace is already an optional statement |
Beta Was this translation helpful? Give feedback.
-
This sounds practical at first but I find it more problematic than file-scoped directives, because:
Even Java, which assumed that a file corresponded to one class, never assumed that a namespace would correspond to a directory. |
Beta Was this translation helpful? Give feedback.
-
As an author of the duplicated item I would like to fill in some things:
namespace default; And please mind that this feature is available currently for razor files and it's really convenient! |
Beta Was this translation helpful? Give feedback.
-
C# code can be compiled without file using Roslyn API. What if there's no actual .cs file but only C# code text? |
Beta Was this translation helpful? Give feedback.
-
I'd rather have an option to declare containing namespace without braces and indentation. |
Beta Was this translation helpful? Give feedback.
-
If this were possible, it would pair very nicely with dotnet/project-system#5372 In the csproj you could do something like: // MyProject/MyProject.csproj
<ItemGroup>
<Folder Include="Models" Namespace="MyProject" />
</ItemGroup> and files in that directory could exclude the namespace declaration: // MyProject/Models/FooBar.cs
using System;
public class FooBar
{
} In this case, |
Beta Was this translation helpful? Give feedback.
-
That would be a breaking change as that is already a valid code, as mentioned above in this thread. I like the idea that namespace would be a property of folder and the tooling would generate new classes in the namespace as specified in the property. If people need files to be moved across folders and change their namespaces accordingly, then this should be apparent from the code. |
Beta Was this translation helpful? Give feedback.
-
It seems to me that the best option should be the following implementation - explicit and implicit(@alrz) namespaces. For example, we have a class
namespace Project.Events // implicit namespace
namespace Project.Messages // explicit namespace |
Beta Was this translation helpful? Give feedback.
-
I know when it comes to developing language features, it's hard to say "keep it simple" because, in my view, it's the very job of this tooling to hide the complications that make KISS possible for folks like me. Best I can do is share my opinion here and hopefully it contributes to the cause. For my day-to-day coding, I tend to keep my namespaces and file structure identical. As far as my work experience goes, this just feels like the de facto industry standard. More importantly, tooling like Resharper is constantly pointing out namespaces that do not match my directory structure. So, I've built up a habit over the years of fixing these "issues". Having to explicitly and redundantly specify namespaces is a hinderance because each time I move a file, I have to "refactor" the namespace, which affects the file itself as well as anywhere that uses it. If I take a few steps back, I'm not even sure why this convention exists. I come from mainly a C++ background and in that domain, the best practice was to always give a name to namespaces that represents the logical grouping or package of the code. I rarely let directories directly influence the namespace for at least the reason above: When a source file moves, there's no reason for the code to "break". Maybe it's counter-productive to focus on the language here and instead ask ourselves why tooling like Resharper is doing this and why the convention matters. I'm a bit torn on this, to be honest. However, the conventions I must follow are (most of the time) not something I can change. So I do think it would be helpful for the language to assist in this convention, if it's reasonable to do so. And I only suggest that because of how widely this convention seems to be used; I'm very doubtful this is a fringe issue. |
Beta Was this translation helpful? Give feedback.
-
Another syntax suggestion: implicit namespace; |
Beta Was this translation helpful? Give feedback.
-
That appeals to me because I almost NEVER want directory / namespace to vary. Currently visual studio has a nice feature to automatically change namespaces when you drag files around -- if it could show the namespace to side of that, it would work for almost every file. Not sure if the practice is allowed, but if the compiler would freshly update implicit namespace; to implicit namespace MyProgram.Weather.Forecasting; based on folder and then if I move it elsewhere even outside of visual studio, on compile the namespace updated to the new folder (or if that's a no-no a warning generated that namespace does not match), I'd probably use it. I would also probably leverage such an option as a project level option for all namespaces if it existed. |
Beta Was this translation helpful? Give feedback.
-
I don't think this is a good idea: it makes source rely on filesystem and directory structure, which is not always present or reliable. |
Beta Was this translation helpful? Give feedback.
-
Here's my take: this is not terrible an idea exactly because it would be opt-in. Moving files around during an API change should not be a breaking change for many, and in most cases you want to be structuring your project minimizing the risk of eventually getting it adjusted. You may specify the namespace declaration implication only on files that are the least likely to be moved elsewhere, or not at all. Both VS and Rider already provide a refactoring that automatically changes the namespace, and also apply it on files whose namespaces are already matching the folder structure. For me personally, I rarely move files around after having pushed a release. Having something like Also, the IDEs could support hinting the resulting namespace for the current placement of the source file in the project. |
Beta Was this translation helpful? Give feedback.
-
Since global usings are already a compiler switch, I’d prefer this to be a compiler switch instead of a new directive in each file. Otherwise, you’re leaving behind unnecessary boilerplate. |
Beta Was this translation helpful? Give feedback.
-
I would love to see this as an opt-in project setting (e.g. |
Beta Was this translation helpful? Give feedback.
-
I really think that it's something that the IDE should handle and there's no need to have this coupling between the file system and namespaces even though that generally I like to have them in sync. p.s. Next someone might suggest implicit types names based on files names. 😄 |
Beta Was this translation helpful? Give feedback.
-
Keeping namespaces in sync with directory structure is usually a convention most of codebases follow. I think such feature would make that easier especially when public API contract is not an issue.
A simple syntax such as
namespace;
is needed to hint the compiler to choose a namespace based on directory structure.Note: #2546 would only decrease nesting, but directory namespaces would make it entirely implicit.
Beta Was this translation helpful? Give feedback.
All reactions