I'm writing code that imports values from a CSV file. As part of the import process, I need to write warnings to a log (displayed on screen) when values in the CSV file are invalid.
The method below takes a string value, and a maximum value to check against.
My concern is that the method does two things: it parses a value, and it generates log entries to display. Does this violate the single responsibility principle, and if so, what can be done about it without duplicating the same number-check logic in two methods?
private double? GetValidDoubleInMinMaxRange(string importVal, double? maxScore)
{
if(importVal == null)
{
return null;
}
double val;
if(double.TryParse(importVal, out val))
{
if(val < 0)
{
AppInstance.Log.Add(this, LogLevel.Warning, Resources.DataScreenImport_ValueMustBePositive);
return null;
}
if(maxScore.HasValue && val > maxScore.Value)
{
AppInstance.Log.Add(this, LogLevel.Warning, Resources.DataScreenImport_ValueGreaterThanMax);
return null;
}
return val;
}
else
{
AppInstance.Log.Add(this, LogLevel.Warning, Resources.DataScreenImport_ValueWasNotValidInt);
return null;
}
}