How to verify variables that are not used?

Asked

Viewed 931 times

6

Is there any way to verify which variables are not used within the scope one-class?

As in the example below, the variables teste1 and teste4 has no utility within the application, as could locate such variables?

using System;
using System.Web.UI;
namespace bDBContext
{
    public partial class _Default : Page
    {
        string teste1 = ""; // não usada
        bool ok = true; // usada
        string teste2 = ""; // usada

        protected void Page_Load(object sender, EventArgs e)
        {
            string teste3 = teste2; // usada

            if (ok)
            {
                string teste4 = ""; // não usada
            }
        }

        public void Salvar()
        {
            string teste5 = ""; // usada
            if (string.IsNullOrEmpty(teste4))
            {

            }
        }
    }
}

Something doesn’t have to use paid third-party tools like Resharper.

  • 2

    Give more details. Serve anything in VS? Any edition/version? Can other tools be free? Can it be something written by you? Are you talking about class fields? Not method variables? Have an example?

  • @bigown, I edited the question.

  • I will see all the options below before choosing the one that doesn’t suit.

4 answers

5


Visual Studio Input Versions

There is nothing embedded in Visual Studio (in the incoming versions) except warnings during compilation. These warnings inform the variables never used (note that a private variable that is assigned within a constructor but is no longer used outside of it is not reported as unused, as far as I know).

You can double-click each warning that will be taken to the code line where the variable, or unused method(o) has been declared.

The ideal is to use a tool, and the best is Resharper, but as it is paid, maybe a solution is to use Fxcop which is free: https://www.microsoft.com/en-us/download/details.aspx?id=6544

Premium versions of Visual Studio

For the Premium and Ultimate versions of Visual Studio 2012, 2013 and 2015 the functionality of Static Code Analysis was derived and evolved from Fxcop, so if you already have a premium version of Visual Studio, you should have access to these features.

In my Visual Studio 2012 Ultimate I access code analysis by pressing ALT+F11 or right-click as shown in the image below:

Selecionado Code Analysis no Visual Studio Ultimate 2012

In the code analysis execution screen you’ll probably need to configure which analysis rules you want to use (putting them all in is cool, but it might be awesome). To pick unused variables I chose the option displayed in the image below:

inserir a descrição da imagem aqui

5

There is a free extension that does this:Codemaid

She’s not as complete as the Resharper, but adds several shortcuts that do not exist natively in Visual Studio.

Works with express and community versions.

  • Could indicate where you have information about it that does what’s in the question?

4

Depends on the compiler configuration, but already does it by default.

If it is private field the compiler can issue Warning. Especially the CS609 and CS414 warn about unused fields, without and with value, respectively.

Public camps get more complicated because it can be normal in the class that it is not used within the class. About warning not to use internal fields already was debated on.

Local variables are alerted with the CS0219 and the CS0168.

It is also possible to use Microsoft’s code analysis tool, in which case it would be the Warning CA1823. But I see no need, it does no more than the compiler already does for this case.

There are other warnings who can verify similar things.

In public fields it is much more complicated to check, there are cases that doesn’t even make sense. But it is possible. That’s one of the reasons I didn’t dismiss Resharper.

3

I would recommend using the Code Cracker.

It is an extension for code analysis that acts directly as a Roslyn Analyzer. In addition, it is open-source and its code is available on Github

Finally, it is developed by Brazilians, and the people who created the extension are very receptive and quiet to talk and help, and very active in the community.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.