I am writing a C# script to count all the 'if...then' statements present in 4000 lines of SAS code. I am taking the SAS code as a text file and then performing my operation. Here's my C# code:
class Program
{
static void Main(string[] args)
{
List<String> list = new List<string>();
list = File.ReadAllLines(@"C:\Users\Dey\Documents\Trucks\file.doc").ToList();
int count = CountIfs(list);
Console.ReadKey();
}
static int CountIfs(List<String> list)
{
int count = 0;
var index = new List<int>();
var value = new List<String>();
for (int i = 0; i < list.Count; i++)
{
if (list[i].Contains("if"))
{
if (list[i].Contains("then") || list[i + 1].Contains("then"))
{
count++;
index.Add(i);
String temp = null;
if(list[i].Contains(";"))
temp = list[i].Substring(0, list[i].IndexOf(";"));
if (temp != null)
{
value.Add(temp);
}
}
}
}
return count;
}
}
I'm essentially checking for "if" per line and if that is found, i'm checking for "then". And finally I'm also making sure its one line of code by checking for ";" . My logic is that if all these conditions satisfy, then I am indeed able to extract all the if-statements from the code. The list "value" would contain all the "if" statements found in the code. Now I've output "value" to a textfile in notepad and have manually cross-checked about 20 entries. And so far it seems to be working alright. I was hoping someone can proof-read my code and tell me if I can improve it somewhere perhaps? Or is this logic sufficiently accurate?
DoSomething
? \$\endgroup\$DoSomething
your actual method name, or is it pseudo-code? Note that questions about pseudo-code are off-topic on Code Review. \$\endgroup\$