Written July 27, 2009 at 13:40 MDT Tagged c sharp and programming
During class last week someone had written a piece of code right near the end of the last evening and I committed to showing them a potential refactoring that could be used. Here is the piece of code in question:
var info_list = new List<ResolverConfigurationInfo>(parser());
var resolver_items = info_list.Select(x => factory(x)).ToList();
var index = 0;
parser().each(item =>
{
resolvers.Add(item.AbstractType, resolver_items[index]);
index++;
});
var info_list = new List<ResolverConfigurationInfo>(parser());
var resolver_items = info_list.Select(x => factory(x)).ToList();
info_list.union(resolver_items).each(item => resolvers.Add(item.first_value.AbstractType, item.second_value));
public class Tuple<FirstType, SecondType>
{
public FirstType first_value { get; private set; }
public SecondType second_value { get; private set; }
public Tuple(FirstType first, SecondType second_type)
{
this.first_value = first;
this.second_value = second_type;
}
}
static public IEnumerable<Tuple<FirstType, SecondType>> union<FirstType, SecondType>(this IEnumerable IEnumerable { Check.not_null(first_set,second_set); var first_list = first_set.ToList(); var second_list = second_set.ToList(); Check.ensure(first_list.Count == second_list.Count); for (var index = 0; index < first_list.Count; index++) yield return new Tuple<FirstType, SecondType>(first_list[index], second_list[index]); }