How to deal with elimination of duplicate logic vs. cost of complexity increase?
- by Gabriel
I just wrote some code that is very representative of a recurring theme (in my coding world lately): repeated logic leads to an instinct to eliminate duplication which results in something that is more complex the tradeoff seems wrong to me (the examples of the negative side aren't worth posting - but this is probably the 20th console utility I've written in the past 12 months).
I'm curious if I'm missing some techniques or if this is really just on of those "experience tells you when to do what" type of issues.
Here's the code... I'm tempted to leave it as is, even though there will be about 20 of those if-blocks when I'm done.
static void Main(string[] sargs)
{
try
{
var urls = new DirectTrackRestUrls();
var restCall = new DirectTrackRestCall();
var logger = new ConsoleLogger();
Args args = (Args)Enum.Parse(typeof(Args), string.Join(",", sargs));
if (args.HasFlag(Args.Campaigns))
{
var getter = new ResourceGetter(logger, urls.ListAdvertisers, restCall);
restCall.UriVariables.Add("access_id", 1);
getter.GotResource += new ResourceGetter.GotResourceEventHandler(getter_GotResource);
getter.GetResources();
SaveResources();
}
if (args.HasFlag(Args.Advertisers))
{
var getter = new ResourceGetter(logger, urls.ListAdvertisers, restCall);
restCall.UriVariables.Add("access_id", 1);
getter.GotResource += new ResourceGetter.GotResourceEventHandler(getter_GotResource);
getter.GetResources();
SaveResources();
}
if (args.HasFlag(Args.CampaignGroups))
{
var getter = new ResourceGetter(logger, urls.ListCampaignGroups, restCall);
getter.GotResource += new ResourceGetter.GotResourceEventHandler(getter_GotResource);
getter.GetResources();
SaveResources();
}
}
catch (Exception e)
{
Console.WriteLine(e.InnerException);
Console.WriteLine(e.StackTrace);
}