Proposal: Multiple imports per using directive #1696
Replies: 42 comments
-
@bondsbw The general idea is nice but tbh I really find this |
Beta Was this translation helpful? Give feedback.
-
The issue with this kind of proposals is that if you have this many using statements:
Then it might be a hint you need to refactor and even then I'm not I'm sure it's worth it. Just my 2c. |
Beta Was this translation helpful? Give feedback.
-
The braceless syntax is good too. (Might need to think about whether #218 and other proposals for |
Beta Was this translation helpful? Give feedback.
-
Similar code is more likely to use similar imports. More likely to see this after a good refactoring: using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml; than this: using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Xml; So even though there are fewer imports, they would be the ones more likely to benefit from reduction. |
Beta Was this translation helpful? Give feedback.
-
This is just a restatement of the proposal. :)
TBH, this just doesn't seem to be that useful/important. |
Beta Was this translation helpful? Give feedback.
-
I mean, just looking at your example:
These namespaces are wildly different. WHy does having them all in one line make thigns easier to review than just seeing:
In both cases i can see you're using 5 wildly different namespaces... how does that help me? How does consolidation make it any better? -- Another issue is that i don't see how it helps me to know those 5 namespaces are used in one line... but then these two namespaces are called out in different line:
Why is IO/Xml/Diagnostics really important to see together... but seeing Collections.Generic is not... Basically... i'm not actually seeing how this makes things any clearer when i'm going to read a PR. |
Beta Was this translation helpful? Give feedback.
-
What I meant was to contrast with using System.Reflection;
using System.Text;
using System;
using System.Windows;
using System.Windows.Controls;
using MyStuff;
using MyStuff.CollectionsEx;
using Logger;
using System.Threading.Tasks;
using System.Windows.Input;
using TheirStuff.Net;
using System.Xml;
using System.Windows.Media;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO; |
Beta Was this translation helpful? Give feedback.
-
That's fair. I considered adding something like this to the proposal: using System.{ Collections.Generic, Threading.Tasks }; |
Beta Was this translation helpful? Give feedback.
-
using System.Reflection;
using System.Text;
using System;
using System.Windows;
using System.Windows.Controls;
using MyStuff;
using MyStuff.CollectionsEx;
using Logger;
using System.Threading.Tasks;
using System.Windows.Input;
using TheirStuff.Net;
using System.Xml;
using System.Windows.Media;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO; This would be infinitely more readable if you ordered them appropriately: using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml;
using Logger;
using MyStuff;
using MyStuff.CollectionsEx;
using TheirStuff.Net; |
Beta Was this translation helpful? Give feedback.
-
Don't you think that it's simple enough? |
Beta Was this translation helpful? Give feedback.
-
@eyalsk, it isn't that uncommon, especially when dealing with the UI stacks (XAML, WinForms, etc). |
Beta Was this translation helpful? Give feedback.
-
@tannergooding If you have that many namespaces trust me, you're doing it wrong. |
Beta Was this translation helpful? Give feedback.
-
Nah, I've seen that many times. It happens quite often. |
Beta Was this translation helpful? Give feedback.
-
@eyalsk, no. It depends on your dependencies/etc. Even just going through Roslyn, there are plenty of files that have ~7 usings and quite a few that have over 12. Such as: http://source.roslyn.io/#Microsoft.VisualStudio.LanguageServices.Xaml/Implementation/XamlTextViewCreationListener.cs,20e58dc1b7876041 When you are building UI components that depend on other layers of the stack, it is quite easy to have imports jump up. I still am not a favor of this proposal, as I think having the IDE add/sort usings for you is sufficient; but having over 10 usings is not terribly unusual. |
Beta Was this translation helpful? Give feedback.
-
Agreed. And note that this is something the IDE will do for you automatically now if you just 'format document': |
Beta Was this translation helpful? Give feedback.
-
Oh for sure - just throwing out ideas (as mentioned in the post) |
Beta Was this translation helpful? Give feedback.
-
if you don't want to see the namespace's list, just put them in a region
|
Beta Was this translation helpful? Give feedback.
-
@Mteheran, that requires an IDE (which already lets you collapse sequential using statements). |
Beta Was this translation helpful? Give feedback.
-
That's not the point of the proposal. |
Beta Was this translation helpful? Give feedback.
-
One of hte points was: |
Beta Was this translation helpful? Give feedback.
-
in that case, I propose this syntax:
|
Beta Was this translation helpful? Give feedback.
-
As someone reading the code, how is that easier to understand than just having three separate imports? What does this do to benefit me as a consumer/maintainer of this code? |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi Mostly the same argument as object and collection initializers. Grouping and DRY. (This comment is about the original proposed syntax. Not @Mteheran's.) |
Beta Was this translation helpful? Give feedback.
-
Those arguments don't hold those. With Obj/Collection initializers what i get is the ability to know that i've created an actual instance at runtime that is fully initialized to that state i've provided. This is especially valuable as previously that may have taken many statements, but i'm in a context that only allows an expression. i.e.: Foo(new Whatever { X = 1, Y = 2 ... }); The same does not apply to namespace/imports. Imports do not represent an 'instance'. There is no situations where i'm hampered by the need to do multiple imports over having a single one that would represent hte 'complete entity'. Those guys also let me express the idea of "i don't want anyone to be able to access this value until initialization is complete". With statement-oriented initialization, there is nothing blocking me from accidentally leaking/using the variable prior to finishing this up. With initializers, i can express that idea simply. Again, no such concept holds for namespaces/imports. They're simply a way to bring names into scope. There is no concept of "the names should not be in scope until i specify all the sub-namespaces i want to bring in", nor would there need to be :) |
Beta Was this translation helpful? Give feedback.
-
Ha, I knew someone would bring that up. Initializers are often used in the context of direct assignment. They are a popular replacement for multiple assignment statements, regardless of their ability to be used in any expression context. I doubt it's all about preventing leakage. I really do think there is a readability improvement with such blocks. |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
@asyncawaitmvvm Emphasis on the word might but more importantly emphasis on your attitude. |
Beta Was this translation helpful? Give feedback.
-
Ha, if you dislike folk then take up ten lines in a new file for an enum, you'd really hate my code, as four lines is enough for me, and three of those are for the namespace block 🤣 |
Beta Was this translation helpful? Give feedback.
-
... which often triggers CA1502. I have yet to see a method that has low cyclomatic complexity whilst drawing from many domains, i.e. many |
Beta Was this translation helpful? Give feedback.
-
I appreciate the passionate discussion but I don't appreciate your tone towards others in the community (one example, second example). This behavior is a violation of our code of conduct. If you keep doing this, we might have to block your account, which I really don't want to do. However, we need to ensure that we can have a productive discussion with our community and the technical aspects are challenging enough that we cannot tolerate abusive behavior. |
Beta Was this translation helpful? Give feedback.
-
Multiple namespace imports from a common base namespace are often used together:
This is a proposal to add a shorthand block syntax to the
using
directive in order to declare multiple imports at once:Advantages
The cleaner syntax encourages grouping of similar namespaces together and reduces the line count. Both factors improve the ability to review the namespace import list.
Similar Proposal
#1421 provides a similar feature with similar syntax. But according to comments, its nesting capability and usage of
this
keyword were deemed confusing.The syntax from my proposal is based on other comments on that post (#1421 (comment) and #1421 (comment)) which eliminate nesting and
this
. These changes were viewed favorably based on comment votes.I believe that creating a new proposal for these differences is warranted so that they do not get lost under a proposal which received many down votes.
Beta Was this translation helpful? Give feedback.
All reactions