Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A slightly better refactoring for SLAP
public class SomeClass
{
List<string> extractedValues = new List<string>();
public void DoSomeWork(string inputFile)
{
using (var file = OpenInputFile(inputFile))
{
while (MoreDataExists(file))
{
var nextLine = ReadNextLine(file);
ProcessLine(nextLine);
}
}
}
void ProcessLine(string inputLine)
{
var inputData = GetFieldData(inputLine);
var desiredValue = ExtractDesiredValue(inputData);
AddValueToList(desiredValue);
}
StreamReader OpenInputFile(string inputFile)
{
return new StreamReader(inputFile);
}
string[] GetFieldData(string inputLine)
{
return inputLine.Split(',');
}
string ExtractDesiredValue(string[] fields)
{
return fields[3];
}
bool MoreDataExists(StreamReader reader)
{
return reader.Peek() >= 0;
}
void AddValueToList(string value)
{
extractedValues.Add(value);
}
string ReadNextLine(StreamReader reader)
{
return reader.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment