I started out with a function having code duplication like this:
private static void GetAndSaveResources(string url)
{
...
if (url.Contains("[" + "access_id" + "]"))
{
RestCall.UriVariables.Add("access_id", 1);
}
if (url.Contains("[" + "affiliate_status" + "]"))
{
RestCall.UriVariables.Add("affiliate_status", "approved");
}
...
}
Then, because I had copy/pasted to make that code, I started trying to eliminate that duplication.
First I extracted a method:
private static void ConfigureUriVariable(string url, string variable, object value)
{
if (url.Contains("[" + variable + "]"))
{
RestCall.UriVariables.Add(variable, value);
}
}
However, while that eliminated the duplication it also created a duplication of string url
accross two functions so I futher reduced it to:
Action<string,object> setUriVariable = (variable, value) =>
{
if (url.Contains("[" + variable + "]"))
{
RestCall.UriVariables.Add(variable, value);
}
};
setUriVariable("access_id", 1);
setUriVariable("affiliate_status", "approved");
However, I would have been confused by that code before I learned about lamdas so I wonder, was the last step a step in the wrong direction?