0

I've been tasked with cleaning up a program that was written by someone who no longer works for us. The previous programmer did not comment most of his code and much of it is unused. He would use a line or two from random online files or APIs but would not discard what he didn't need, making the program quite robust. I could go file by file and delete everything that's not referenced but there are thousands of files and some of them are 10's of thousands of lines. That would be quite tedious. My thought process was to create an application that would trace through each .cs file, copy unreferenced code to a separate file with the file name and line number it came from, and then delete that unreferenced code from the program.

So far, I've managed to collect every .cs file into a string array. I've been able to discard both the temporarily generated and assembly files from this array that do not need parsing and have concatenated the array accordingly. I was going to go through this array, file by file, testing each iteration for unused code by calling a method called Parse. The idea being that this method would be able to trace through files and depict if something is a method, class, or neither. For this functionality I was thinking about testing each individual line if there were certain keywords such as "class" or "public static..." If it were deemed to be a method/class, another method, IsReferenced, would be called that would return true or false based upon whether or not the method/class that is being tested actually has any references within the program. If I can get to this point it would be as simple as copying that section of code to another file and documenting it. However, the hard part of this application, personally, would be testing if something is a method OR class AND also testing if it is referenced or not.

// string filePath is the file path for the file that's being traced
public static bool Parse(string filePath)
{

    // Iterates until end of file
    using (StreamReader reader = File.OpenText(filePath))
    {
        string currLine;

        // While there are more lines to read, keep tracing code
        while ((currLine = reader.ReadLine()) != null)
        {
            // Test to see if "class" is in the header and that the 
            // line does not end with a ;
            if ((currLine.Contains("class")) && (!currLine.Contains(";")))
            {
                // now we need to see if it's referenced (used) or not
                if(IsReferenced())
                {
                    return true;
                }
            }

            // must be either a boolean statement or an instantiation 
            // statement
            if ((currLine.Contains("=")) || (currLine.Contains("==")))
            {
                    return false;
            }
        }
    }
}

So far there are no results to show or express. I wanted to ask if there was a better way to go about this before I dove head first into this idea. I'm aware there is software out there that could help with this issue or even perform these exact duties but, it's a long process to download any third party software where I am. Any help at all would be appreciated, thanks.

So far there are no results to show or express. I wanted to ask if there was a better way to go about this before I dove head first into this idea. I'm aware there is software out there that could help with this issue or even perform these exact duties but, it's a long process to download any third party software where I am. Any help at all would be appreciated, thanks.

  • 4
    If you actually want to programatically work with source code, you should use a tool that actually understands it, such as the [Roslyn compiler](https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/get-started/syntax-analysis). However, I think you'll be far better of running through this code by hand and analyzing each bit you want to clean up, rather than trying to automate it. – mason Jul 08 '19 at 17:06
  • Possible duplicate of [Visual Studio Find All Not Referenced](https://stackoverflow.com/q/17855741/11683) – GSerg Jul 08 '19 at 17:06
  • Here's something current: https://stackoverflow.com/questions/30974433/get-list-of-zero-reference-codes-in-visual-studio. The big caveat is whether the code uses lots of reflection, which means that classes and methods are discovered at runtime. Just hope that isn't the case, because it could make this much more difficult. The available tools could tell you that code isn't used when it really is. If he copied lots of code off the internet I'd be concerned. – Scott Hannen Jul 08 '19 at 17:18
  • Resharper can remove unused code, but I don't think you can copy the unused code to a file by using it – Sohaib Jundi Jul 08 '19 at 17:24
  • Possible duplicate of [Get List of Zero Reference Codes in Visual Studio](https://stackoverflow.com/questions/30974433/get-list-of-zero-reference-codes-in-visual-studio) – Ken White Jul 08 '19 at 20:52

0 Answers0