6
\$\begingroup\$

How can I simplify this code?

 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...

asked Nov 20, 2012 at 12:51
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Jumping in after the horse has bolted with a very minor different approach (using Any rather than Count and null).

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);
}

Then used such as

IEnumerable<string> smallFileNames = GetSmallFileNames(directory, fileName, "*.csv");
answered Nov 20, 2012 at 21:11
\$\endgroup\$
2
  • \$\begingroup\$ Isn't Count faster than Any()? Maybe that wasn't your point though. \$\endgroup\$ Commented Nov 23, 2012 at 14:52
  • \$\begingroup\$ Yes it probably is. But I would think the speed is actually so small that in this case it's irrelevant. Also people often mix .Count with .Count() which are subtly different. So .Any() just removes that confusion.... \$\endgroup\$ Commented Nov 23, 2012 at 18:39
1
\$\begingroup\$

...and you could simplify the second conditional by initializing smallFileNames :

List<string> smallFileNames = new List<string>
if (directory.Exists)
{
 smallFileNames = directory.GetFiles("*.csv").Select(i => i.FullName).ToList();
}
if (smallFileNames.Count == 0)
 smallFileNames = DivideIntoFiles(fileName);

However, the whole logic seems unclear to me: there seems to be a hidden relation between directory and fileName. I would consider reviewing the surrounding code also.

answered Nov 20, 2012 at 19:00
\$\endgroup\$
4
  • \$\begingroup\$ You could also do something like this: var smallFileNames = directory.Exists ? directory.GetFiles("*.csv").Select(i => i.FullName).ToList() : new List<string>(); \$\endgroup\$ Commented Nov 20, 2012 at 19:15
  • \$\begingroup\$ But it would always create a list, even when not needed. \$\endgroup\$ Commented Nov 20, 2012 at 20:23
  • 2
    \$\begingroup\$ I'm a big fan of always creating a list. Takes out the if (list == null) checks every time you want to use it. An empty list is just that, empty, so an iteration or check if there are values will fail if there are any. \$\endgroup\$ Commented Nov 20, 2012 at 22:08
  • \$\begingroup\$ hidden relation between directory and fileName Thank you. I think it's good idea. \$\endgroup\$ Commented Nov 21, 2012 at 6:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.