How to find unused methods?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank Rizzo

    How to find unused methods?

    Is there a setting in VS2005 to quickly locate methods that are unused
    (maybe through compiler warnings)? If not, any utilities out there that
    do that?

    Thanks
  • Egghead

    #2
    Re: How to find unused methods?

    hmm,
    Are you doing unit test and code coverage?
    You have $$$ to burn, you can try the VS team's developer and tester (I do
    not use it).
    If no $$ to burn, google "unit test+code coverage+C#."

    --
    cheers,
    RL
    "Frank Rizzo" <none@none.comw rote in message
    news:OyQouU3gHH A.284@TK2MSFTNG P05.phx.gbl...
    Is there a setting in VS2005 to quickly locate methods that are unused
    (maybe through compiler warnings)? If not, any utilities out there that
    do that?
    >
    Thanks

    Comment

    • Andy

      #3
      Re: How to find unused methods?

      On Apr 20, 1:56 pm, Frank Rizzo <n...@none.comw rote:
      Is there a setting in VS2005 to quickly locate methods that are unused
      (maybe through compiler warnings)? If not, any utilities out there that
      do that?
      Well, you can right click on the definition and choose Find All
      References, although this only works at the solution level.

      Another alternative might be to use Reflector.Net and open all the
      assemblies in your application, then use the Analize feature.

      Comment

      • FourEyes

        #4
        Re: How to find unused methods?

        On Apr 20, 1:56 pm, Frank Rizzo <n...@none.comw rote:
        Is there a setting in VS2005 to quickly locate methods that are unused
        (maybe through compiler warnings)? If not, any utilities out there that
        do that?
        >
        Thanks
        TestDriven.NET NCover should do it, but I've never used it.

        Comment

        • atlaste

          #5
          Re: How to find unused methods?

          I think some context is missing in this discussion. There isn't any
          way to do what you want. And for good reason.

          For example think about the case that you a couple of methods and
          classes that are only called resp. instantiated by using reflection.
          There's no telling which methods aren't used and which methods are in
          general.

          In the specific case that you have a "bounded application" with just a
          couple of DLL's and executables you can try using code coverage or
          other tests -- but that might prove to be problematic because not
          every method has to be called during a test run.

          So that leaves you just with one possible option: trying to figure out
          the dependencies of a method by creating a program.

          Perhaps I can help with a starting point. Let's assume you don't use
          reflection. Then you can actually exploit reflection of .NET to find
          assembly references:
          1. use '<assembly>.Get ReferencedAssem blies()'; push all those in a
          dictionary and recurse.
          2. compare the assemblies in the dictionary to the assemblies in the
          directory. That leaves you with unused assemblies.

          Furthermore you should notice that methods are usually called from
          within classes. Ask every assembly for its types and iterate over all
          methods. You will end up with a bunch of "MethodInfo " objects. Find
          the "GetMethodB ody" function and loop through the list of
          "LocalVariables " and find the "LocalType" member of that. Combine that
          with the parameter types, generic types, inheritance types and the
          attribute types and you will get a list of all types that the method
          uses (hope I'm not forgetting something...). Compare that to the types
          you find in all assemblies. You will end up with the classes that
          aren't used.

          .... and to end up unleashing all hell I would try to parse the IL code
          in the GetMethodBody function. After all, how hard can that be? :-)
          (http://www.chiramattel.com/george/blog/2004/08/16/
          first_version_o f_il_parser.htm l)

          Perhaps it's easier to just leave the unused methods to rot where they
          were in the first place...

          Cheers,
          Stefan.

          P.S.: if you decide to turn to code coverage you can always try (free
          trial) ANTS profiler, (free) CLR profiler, or the likes. While it does
          way more than you want, you can deduce most of the information you
          seek there as well.

          On 20 apr, 19:56, Frank Rizzo <n...@none.comw rote:
          Is there a setting in VS2005 to quickly locate methods that are unused
          (maybe through compiler warnings)? If not, any utilities out there that
          do that?
          >
          Thanks

          Comment

          • Chris Nahr

            #6
            Re: How to find unused methods?

            On Fri, 20 Apr 2007 10:56:09 -0700, Frank Rizzo <none@none.comw rote:
            >Is there a setting in VS2005 to quickly locate methods that are unused
            >(maybe through compiler warnings)? If not, any utilities out there that
            >do that?
            I believe FxCop issues warning for unused methods, as well as unused
            variables.
            --
            Stay ahead in World of Warcraft with expert guides, latest patch news, class tips, dungeon strategies, PvP builds, and The War Within updates—all in one place.

            Comment

            • Peter Macej

              #7
              Re: How to find unused methods?

              MZ-Tools (http://www.mztools.com/index.htm) add-in can do it. Use
              "Review Dead Code..." functionality. This will also show you unused
              variables and exceptions.

              --
              Peter Macej
              Helixoft - http://www.helixoft.com
              VSdocman - Commenter and generator of class documentation for C#, VB
              ..NET and ASP .NET code

              Comment

              • Walter Wang [MSFT]

                #8
                RE: How to find unused methods?

                Hi Frank,

                First, here's an answer from Eric Gunnerson on why C# doesn't warn about
                unused methods:

                #C# Frequently Asked Questions : Why doesn't C# warn about unused methods?



                Second, Fxcop's rule "Avoid uncalled private code" also only checks on
                private or internal (assembly-level) member that does not have callers in
                the assembly, is not invoked by the common language runtime, and the member
                is not invoked by a delegate. Also, there's another rule called "Avoid
                uninstantiated intenral classes" may help. However, the rules can report
                false positives if there are entry points that are not currently identified
                by the rule logic.

                For more information on Fxcop, please see


                Hope this helps.


                Sincerely,
                Walter Wang (wawang@online. microsoft.com, remove 'online.')
                Microsoft Online Community Support

                =============== =============== =============== =====
                Get notification to my posts through email? Please refer to
                Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

                ications. If you are using Outlook Express, please make sure you clear the
                check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
                promptly.

                Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
                where an initial response from the community or a Microsoft Support
                Engineer within 1 business day is acceptable. Please note that each follow
                up response may take approximately 2 business days as the support
                professional working with you may need further investigation to reach the
                most efficient resolution. The offering is not appropriate for situations
                that require urgent, real-time or phone-based interactions or complex
                project analysis and dump analysis issues. Issues of this nature are best
                handled working with a dedicated Microsoft Support Engineer by contacting
                Microsoft Customer Support Services (CSS) at
                http://msdn.microsoft.com/subscripti...t/default.aspx.
                =============== =============== =============== =====

                This posting is provided "AS IS" with no warranties, and confers no rights.

                Comment

                • Frank Rizzo

                  #9
                  Re: How to find unused methods?

                  Peter Macej wrote:
                  MZ-Tools (http://www.mztools.com/index.htm) add-in can do it. Use
                  "Review Dead Code..." functionality. This will also show you unused
                  variables and exceptions.
                  Thanks. I actually have a license and it's installed in my VS2005
                  instance, just didn't notice the feature.

                  Regards

                  Comment

                  Working...