-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: File scoped namespaces #2546
Comments
I think this is an excellent idea which should be very easy to implement. Just one point. As using directives can be local to a namespace, there would be a difference in meaning between: namespace Foo;
using System.Collections.Generic;
public class Bar
{
...
} and using System.Collections.Generic;
namespace Foo;
public class Bar
{
...
} In the first case, the using statement would be local to Foo and, in the second case, it would be global . However, there would be no practical difference between the two if no other namespaces were allowed in the file as specified in your proposal. In the interests of simplicity, it might therefore be best to require that the namespace declaration should always be the first line in the file. |
@alanfo The idea is that the namespace is file-scoped, so the question of whether a However, I do think that the rules for such a file-scoped namespace should probably mirror the rules for |
What about the case of a partial class spread across multiple files, having different file-scoped namespaces |
Wouldn't it ultimately end up meaning the same thing? File scoped just means the entire file lives in that namespace rather than allowing multiple. |
@MgSam @AdamSpeight2008 @RichiCoder1 |
This is what I hoped from version 1. it is not only wasting space, it also makes extra level of brace, where you'll need some IDE plugins make it easy to tell what each brace is for. |
Haha, sorry @alanfo, that was a response to @AdamSpeight2008. I absolutely agree. |
Absolutely support! This is a very nice feature. It is really a waste of indentation to put every class inside a namespace scope. And it is an easy feature. |
The indentation point is actually quite convincing. This is going to be a long shot: all this makes sense because statistically many developers actually have single namespace per file, but many developers also have single class per file and some nested classes. Maybe it's a good idea to allow similar syntax for classes then?
or
|
Very nice proposal. I like the idea of file scope class. It will allow very clean code. |
I don't see any real benefit to this proposal. It saves the developer a few keystrokes per file and removes an extra level of indentation (which any decent IDE can do automatically) but doesn't improve readability. |
Having something scoped to the compilation unit (file) is nothing new to C#. Because there are differences between compilation unit scoped and namespace scoped The using keyword is used for the using directive and the using statement but de difference is clear, not only because of where they are used, but how they are used. The using directive is a directive (there aren't many of them in C#) and has no body and the using statement has a body. If fact, all statements introduced in C# (or not present is languanges commonly used at the time) that have associated statements/bodyes ( This proposal doesn't seem to fit well with C#. If you really feel that those characters lost on the left are that important, I propose that the namespace declaration might be used without the curly braces, meaning that it will apply to whatever is next:
Or, beacause that space matters:
But, then, it becomes just about formatting (tooling):
Does that really worth all the hassle of the change? How about this, instead?
|
I think @paulomorgado proposal is a nice compromise. It can be done, because namespaces do not have any modifiers what makes them different from classes, methods and so on. |
I think this is an implementation detail. Is there a difference in behavior to the user? If not than it doesn't really matter which is used.
This would be both confusing and demonstrably less useful. Style guidelines recommend one class per file, but often you see files with more in them than just a single class. More than one namespace per class, on the other hand, is almost never seen.
Visual Studio has certain defaults for files and auto-formatting behaviors it uses; C# has style guidelines which the vast majority of devs generally follow. All of these include a namespace enveloping the entirety of the file and indentation associated with that namespace. It's nice to talk about a hypothetical world in which this isn't a problem but the fact is the overwhelming majority of code will continue to use the current style unless there's a compelling language-based reason not to. |
For imperative-style it is a minor upgrade, but for functional-style it's a major upgrade. In imperative style you have a lot of short instructions one per line, control structures makes a lot of indents and outdents. In functional approach you have some definitions - sometimes one-per-line each consisting of usually long expression where IDE gives hand with utilities like word-wrap. I think this couple of characters saved on the left will cause further word-wrapped lines to start earlier accumulating saved space on per-line basis. Consequently it means more actual lines visible per page and consequently more options to logically arrange code. It's far more than code will be more on the left. |
I added a link in the proposal to UserVoice, so you can vote for the feature if it interests you. |
§9.4.2 (Using namespace directives) does not mention the differences, but §7.6.5.2 (Extension method invocations) states that:
This is valid for everyting namespace related. Using directives inside a namespace declaration superside any outer using directives. So, this is not a style issue and, definetely, there's a difference to the user. |
Hmm, nested namespaces brings up some interesting issues: namespace Namespace1;
namespace Namespace2 {
class C {}
} Does class C belong to |
@paulomorgado @tenor I think the same part of the spec I proposed answers both concerns:
So binding extension methods is not an issue since all using statements would be within the same namespace scope. It would be unnecessarily confusing and complicating to allow namespace blocks to be declared when a file-scoped namespace is present. |
I like this idea. The forced amount of indentation has been a minor annoyance over the years. I'm not sure I'd be comfortable doing the same thing to the class declaration, but the beauty of this proposal is that it would work either way according to your preferences. I have a possible alternative to the single line
|
That was my proposal, @jnm2. And it's not as strange as one might think because this:
is already the same as this:
|
Oh, my bad. I'm 100% with you there. |
Nice, I like this. Just one max per compilation unit would be enough I think. Some say, saving spaces improve compilation speed. I don't know, but it does improve readability, good for portrait screen coders, too. |
Ouch! |
It would be fantastic if I could specify both namespace and partial class: public partial class MyNamespace.MyClass
{
[...]
} private class MyNamespace.MyClass.UtilityClass
{
[...]
} |
Perfectly agree. It does not make sense to intend on namespace as anyways we can have only 1 namespace in a file. |
We can as it is allowed by the C# grammar. Just because a majority of examples use only one, doesn't indicate that only one is allowed. |
It's not just the majority of examples. It's a well-established code style guidelines, used by .NET Framework itself among other things. If the de-facto official style guide results in unnecessarily verbosity (in this case, wasted indentation) for 99% of lines in every source file, something is clearly wrong. One possibility is that IDE should simply never indent the body of the outermost Otherwise, I think the most reasonable approach is to come up with some simple syntactic sugar - in other words, whatever the syntax is, it should have a straightforward desugaring into the existing Since there are already a few suggestions above, I'm going to add one more: namespace Foo:
class Bar { ... }
enum Baz { ... }
...
namespace Nested {
...
} So basically the same as the proposal, but using There are only two simple rules for desugaring this:
This takes care of the 90% case (single namespace in a file) with straightforward, easy to understand syntax, and avoids adding new rules or creating corner cases. |
Frankly speaking, this feature would have more positive impact on an average codebase than e.g. pattern matching. It's one file per type most of the time, and we remove 2-3 lines of code and a whole indentation level. I only like @jnm2's/@paulomorgado's syntax though. |
BMSEphx Get Outlook for Androidhttps://aka.ms/ghei36 From: dsaf Frankly speaking, this feature would have more positive impact on an average codebase than e.g. pattern matching. It's one file per type most of the time, and we r emove 2-3 lines of code and a whole indentation level. You are receiving this because you commented. |
Frankly I'd even take the ability to do class names long hand even. public class Namespace.ClassName
{
// define class here...
} What ever it take to get the extra white-space out of the left side. Some of us are trying to live within character limited columns (not my idea). |
@MgSam I really like this feature, perhaps you want to port this to csharplang repo? |
@alrz Did they state they're not going to port the issues over and its up to the community? |
@MgSam From what has happened so far, unless it's already championed, yes. |
@MgSam yes please port. |
Shouldn't style conventions prefer using System;
namespace MyNamspace;
public class MyClass { ... } Over namespace MyNamspace;
using System;
public class MyClass { ... } As it looks closer to what we're used to: using System;
namespace MyNamespace
{
public class MyClass { ... }
} And we're already in the habit of checking the line right above the class name to find the namespace. |
What style conventions? |
I like fully qualified naming style such as suggested by @jnm2 and @paulomorgado. This is consistent with type-using grammar, and also with type declaration grammar in MSIL. |
I wrote my thoughts about file-scoped namespace directives here: https://medium.com/@ssg/a-pledge-for-namespace-directives-in-c-15a322504a15 |
Although this proposal is over 5 years old now, I personally do love this idea aswell. Personally I never saw (well, remember) a file, that contained more than one namespace. This would save some indentation in all my files, so I can abuse it for one more nested if to annoy my co-workers in the next pull-request with... ;) |
What about supporting Of course, for completeness, this will require:
|
@mkArtak An upgrade to that model would entail reorganizing your project folder structure (or your code) significantly which is very intrusive in my opinion. File-scoped directives, on the other hand, can even work with a simple refactor (even find & replace perhaps), and would not even change the diff significantly if you omit whitespace. Also, C# namespace directives are case-sensitive while MacOS/Windows file systems are not. My Spider-sense says trouble. |
@ssg, for an existing project scenario you're referring to, you will still be able to specify explicitly the namespace per file. My proposal goes a step beyond that by eliminating the need for specifying the namespace if the name matches the convention |
I like this idea. Pros are less indents and compactness . Other programming languages like Java, Go, Rust, Python do not use namespace braces. I spend a lot of time on review and I get annoyed by the indents and horizontal scrolling. Another advantage is that the code is left-aligned. |
Closing as a duplicate of #137. |
Background
.NET style guidelines recommend having at most one namespace per file. The current namespace syntax only allows block scope, resulting in every file typically having all of its contents (save using statements) wrapped in a namespace block. This leads to both vertical space being wasted (the curly braces used to delimit the block) and more importantly, horizontal space being wasted, with all of the important content indented by an extra tab (if style guidelines are being followed). The indentation from the namespace block conveys no useful information- it is just code bloat.
Proposal
Allow a single namespace declaration per file that has no braces and therefore no block scope. Every class written in the file is a member of this namespace. It would be an error to attempt to introduce a second namespace declaration of any kind (including one with block scope). The single namespace declaration would be valid in the outermost scope only. It would be valid to appear at any part of the file in this scope, although style guidelines would recommend putting it at the top.
Previous Discussion
Code Plex
Uservoice (vote for the feature)
http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/7181921-file-scoped-namespaces
The text was updated successfully, but these errors were encountered: