Friday, August 5, 2011

3 Reasons to Remove Unused Using in Visual Studio

Remove Unused UsingsAn important programming practice is to remove unused Usings for C# (or Imports for Java). Though not removing unused “Using” will (in most cases) not cause problems, there is the small chance that future changes to a packaged “Using” may conflict. This may occur during an upgrade where two objects then contain the same function name, each serving a different purpose.
Secondly, removing unused “Using” is gives fellow programmers sifting through your code a reasonable idea of the purpose of your application. If however you scatter your “Using” section with unnecessary packages, you will give your application in essence a false identity.
However, including unnecessary “Using” will NOT cause a performance difference in compiled languages during runtime. It may however cause the complication to be a second longer, but in this day an age, that is not a problem.
Finally (#3), removing your unused “Using” only takes only a second in Visual Studio. All you need to do is right click in your codebehind file. Go to Organize Usings, and select “Remove Unused Usings”. A better option is to click “Remove and Sort”, to give me a more organized list.


Now An Important question : "How do I remove all Unused Usings from all the class files in my Project, if I have more than 200 class files? Do I need to open all the files and remove all Unused as mentioned above manually for all files?"

If this is your question, then here is a simple answer and an easy solution:

Ans: NO


Solution :


Steps:
1) Open your Visual Studio
2) Open Tools Menu -> Macros -> Macro Explorer (Keyboard ShortCut : Alt + F8)
3) Right Click on MyMacros -> Select New Module
4) After creating new module Replace all default generated code with below code:



Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics


Public Module mdlRemoveAndSortAll
    Public Sub RemoveAndSortAll()
        On Error Resume Next
        Dim sol As Solution = DTE.Solution


        For i As Integer = 1 To sol.Projects.Count
            Dim proj As Project = sol.Projects.Item(i)
            For j As Integer = 1 To proj.ProjectItems.Count
                RemoveAndSortSome(proj.ProjectItems.Item(j))
            Next
        Next
    End Sub


    Private Sub RemoveAndSortSome(ByVal projectItem As ProjectItem)
        On Error Resume Next
        If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
            If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)


                window.Activate()


                projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort")


                window.Close(vsSaveChanges.vsSaveChangesYes)
            End If
        End If


        For i As Integer = 1 To projectItem.ProjectItems.Count
            RemoveAndSortSome(projectItem.ProjectItems.Item(i))
        Next
    End Sub


End Module


5) Save it and Close the editor.

Congrats, You have created a Custom Macro in MS Visual Studio.


Now to use it :

1) Open your solution
2) Select your macro from Macro Explorer window
3) Right Click -> Run

Wait and watch..... All the Unused Usings from all the Class files in your entire solution will be removed.. Enjoy..


If you like this post, please +1 this post from below toolbar, you can also place a comment or vote below.

Post a Comment

Find a cool free stuff everyday

Giveaway of the Day

Hiren Bharadwa's Posts

DotNetJalps