Why remove unused using directives in C#?

后端 未结 10 1602
猫巷女王i
猫巷女王i 2020-11-30 20:10

I\'m wondering if there are any reasons (apart from tidying up source code) why developers use the \"Remove Unused Usings\" feature in Visual Studio 2008?

相关标签:
10条回答
  • 2020-11-30 20:33

    In addition to the reasons already given, it prevents unnecessary naming conflicts. Consider this file:

    using System.IO;
    using System.Windows.Shapes;
    
    namespace LicenseTester
    {
        public static class Example
        {
            private static string temporaryPath = Path.GetTempFileName();
        }
    }
    

    This code doesn't compile because both the namespaces System.IO and System.Windows.Shapes each contain a class called Path. We could fix it by using the full class path,

            private static string temporaryPath = System.IO.Path.GetTempFileName();
    

    or we could simply remove the line using System.Windows.Shapes;.

    0 讨论(0)
  • 2020-11-30 20:33

    This seems to me to be a very sensible question, which is being treated in quite a flippant way by the people responding.

    I'd say that any change to source code needs to be justified. These changes can have hidden costs, and the person posing the question wanted to be made aware of this. They didn't ask to be called "lazy", as one person inimated.

    I have just started using Resharper, and it is starting to give warnings and style hints on the project I am responsible for. Amongst them is the removal of redundant using directive, but also redundant qualifiers, capitalisation and many more. My gut instinct is to tidy the code and resolve all hints, but my business head warns me against unjustified changes.

    We use an automated build process, and therefore any change to our SVN repository would generate changes that we couldn't link to projects/bugs/issues, and would trigger automated builds and releases which delivered no functional change to previous versions.

    If we look at the removal of redundant qualifiers, this could possibly cause confusion to developers as classes our Domain and Data layers are only differentiated by the qualifiers.

    If I look at the proper use of capitalisation of anachronyms (i.e. ABCD -> Abcd) then I have to take into account that Resharper doesn't refactor any of the Xml files we use that reference class names.

    So, following these hints is not as straight-forward as it appears, and should be treated with respect.

    0 讨论(0)
  • 2020-11-30 20:33

    At least in theory, if you were given a C# .cs file (or any single program source code file), you should be able to look at the code and create an environment that simulates everything it needs. With some compiling/parsing technique you may even create a tool to do it automatically. If this is done by you at least in mind, you can ensure you understand everything that code file says.

    Now consider, if you were given a .cs file with 1000 using directives which only 10 was actually used. Whenever you look at a symbol that is newly introduced in the code that references the outside world, you will have to go through those 1000 lines to figure out what it is. This is obviously slows down the above procedure. So if you can reduce them to 10, it will help!

    In my opinion, the C# using directive is very very weak, since you cannot specify single generic symbol without genericity being lost, and you cannot use using alias directive to use extension methods. This is not the case in other languages like Java, Python and Haskell, in those languages you are able to specify (almost) exactly what you want from the outside world. But event then, I will suggest to use using alias whenever possible.

    0 讨论(0)
  • 2020-11-30 20:38

    It also helps prevent false circular dependencies, assuming you are also able to remove some dll/project references from your project after removing the unused usings.

    0 讨论(0)
提交回复
热议问题