patterncsharpMinor
Simplify 'if' condition
Viewed 0 times
simplifyconditionstackoverflow
Problem
How can I simplify this code?
I confused by identical lines...
if (directory.Exists)
{
smallFileNames = directory.GetFiles("*.csv").Select(i => i.FullName).ToList();
if(smallFileNames.Count == 0)
smallFileNames = DivideIntoFiles(fileName);
}
else
smallFileNames = DivideIntoFiles(fileName);I confused by identical lines...
Solution
Jumping in after the horse has bolted with a very minor different approach (using Any rather than Count and null).
Then used such as
IEnumerable GetSmallFileNames(Directory directory, string fileName, string filter)
{
var smallFileNames = new List();
if (directory.Exists)
{
smallFileNames = directory.GetFiles(filter).Select(i => i.FullName);
}
return smallFileNames.Any() ? smallFileNames : DivideIntoFiles(fileName);
}Then used such as
IEnumerable smallFileNames = GetSmallFileNames(directory, fileName, "*.csv");Code Snippets
IEnumerable<string> GetSmallFileNames(Directory directory, string fileName, string filter)
{
var smallFileNames = new List<string>();
if (directory.Exists)
{
smallFileNames = directory.GetFiles(filter).Select(i => i.FullName);
}
return smallFileNames.Any() ? smallFileNames : DivideIntoFiles(fileName);
}IEnumerable<string> smallFileNames = GetSmallFileNames(directory, fileName, "*.csv");Context
StackExchange Code Review Q#18837, answer score: 3
Revisions (0)
No revisions yet.