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.