I've been working on this code for awhile and for some reason all the if's are driving me crazy along with a bunch of repeated code. Is there a nicer more cleaner way to do this?
public Program(string[] args)
{
try
{
WriteToLogFile("Starting ImportTask");
if (args.Length == 0)
{
Import(DateTime.Now,DateTime.Now);
MarkRecordsAsDeleted();
}
else if (args.Length == 1)
{
DateTime dateToImport;
bool isValidDate = DateTime.TryParse(args[0], out dateToImport);
if (isValidDate)
{
Import(dateToImport,dateToImport);
MarkRecordsAsDeleted();
}
else
WriteToLogFile(String.Format("The Import date specified was invalid. - {0}", args[0]));
}
else if (args.Length == 2)
{
DateTime importStartDate;
bool isValidStartDate = DateTime.TryParse(args[0], out importStartDate);
DateTime importEndDate;
bool isValidEndDate = DateTime.TryParse(args[0], out importEndDate);
if (isValidStartDate && isValidEndDate)
{
if (importStartDate > importEndDate)
{
WriteToLogFile(String.Format("Invalid date range provided. Start date = {0} End date {1}",importStartDate,importEndDate));
return;
}
Import(importStartDate, importEndDate);
MarkRecordsAsDeleted();
}
else
WriteToLogFile(String.Format("The Import date specified was invalid. - {0}", args[0]));
}
else
{
WriteToLogFile("Invalid Command Line Parameters Specified");
}
}
catch (Exception ex)
{
WriteToLogFile("Error in Import Process = " + ex.StackTrace);
}
}
What you've got here is a two-argument function that is expecting strings that can parse to dates. If the second argument is missing, you use the first argument; if the first is missing, you use DateTime.Now.
So, write a two-argument function that takes two dates. Call it. Something along the lines of:
switch(args.length) {
case 0: myFunc(DateTime.Now, DateTime.Now); break;
case 1: myFunc(toDate(args[0]), toDate(args[0])); break;
case 2: myFunc(toDate(args[0]), toDate(args[1])); break;
}
By the way, it appears that you are not referring to args[1] in your function; that's probably a bug.
Here's a first pass:
public Program(string[] args)
{
try
{
WriteToLogFile("Starting ImportTask");
DateTime startTime;
DateTime endTime;
GetDateRange(args, out startTime, out endTime);
Import(startTime, endTime);
MarkRecordsAsDeleted();
}
catch (Exception ex)
{
WriteToLogFile("Error in Import Process = " + ex);
throw;
}
}
private static void GetDateRange(string[] args, out DateTime startTime, out DateTime endTime)
{
switch (args.Length)
{
case 0:
startTime = DateTime.Now;
endTime = DateTime.Now;
break;
case 1:
{
DateTime dateToImport;
var isValidDate = DateTime.TryParse(args[0], out dateToImport);
if (!isValidDate)
{
throw new Exception(
String.Format(
"The Import date specified was invalid. - {0}",
args[0]));
}
startTime = dateToImport;
endTime = dateToImport;
}
break;
case 2:
{
DateTime importStartDate;
bool isValidStartDate = DateTime.TryParse(args[0], out importStartDate);
DateTime importEndDate;
bool isValidEndDate = DateTime.TryParse(args[1], out importEndDate);
if (!isValidStartDate || !isValidEndDate)
{
throw new Exception(
String.Format(
"The Import dates specified was invalid. - {0}, {1}",
args[0], args[1]));
}
if (importStartDate > importEndDate)
{
throw new Exception(
String.Format(
"Invalid date range provided. Start date = {0} End date {1}",
importStartDate, importEndDate));
}
startTime = importStartDate;
endTime = importEndDate;
}
break;
default:
throw new Exception("Invalid Command Line Parameters Specified");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With