Skip to content

Instantly share code, notes, and snippets.

@CopperStarSystems
Created January 14, 2017 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CopperStarSystems/95883982871c6a3bbda83efb2b655a49 to your computer and use it in GitHub Desktop.
Save CopperStarSystems/95883982871c6a3bbda83efb2b655a49 to your computer and use it in GitHub Desktop.
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