lukasss
asked on
how to remove duplicate code from my project
I'm writing a PhoneBook project with c# in the console application, I have a many duplicates codes How to remove duplicate code?
for example, I have two methods GetKeyByName and GetKeyByPhoneNumber these codes are just a little bit difference.
I did highlight it part of my code is the difference.
if (contact.Value.Phone == phoneNumber)
for example, I have two methods GetKeyByName and GetKeyByPhoneNumber these codes are just a little bit difference.
I did highlight it part of my code is the difference.
if (contact.Value.Phone == phoneNumber)
if (contact.Value.Name == name)
private int GetKeyByPhoneNumber(string phoneNumber)
{
int findKey = 0;
foreach (var contact in contacts)
{
[b] if (contact.Value.Phone == phoneNumber)[/b]
findKey = contact.Key;
}
return findKey;
}
private int GetKeyByName(string name)
{
int findKey = 0;
foreach (var contact in contacts)
{
[b] if (contact.Value.Name == name)[/b]
findKey = contact.Key;
}
return findKey;
}
Yes, this is the approach.
However, to make it cleaner, I prefer define types (like search type above) as enumerations.
However, to make it cleaner, I prefer define types (like search type above) as enumerations.
public enum TypeOfSearch { Name, Phone };
In this case it becomes (basically I just repeat Andy's code above):
private int GetKeyByPhoneNumber(string searchString, TypeOfSearch type)
{
int findKey = 0;
if (type == TypeOfSearch.Phone)
{
foreach (var contact in contacts)
{
if (contact.Value.Phone == searchString)
findKey = contact.Key;
}
}
else if (type == TypeOfSearch.Name)
{
foreach (var contact in contacts)
{
if (contact.Value.Name == searchString)
findKey = contact.Key;
}
}
else
{
// May be throw ArgumentException - not expected type of search?
}
return findKey;
}
private int GetKeyByName(string name)
{
int findKey = 0;
return findKey;
}
With the next logical evolution being the reflected generic implementation; e.g. -
-saige-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace EE_Q29006842
{
class Program
{
static void Main(string[] args)
{
var contacts = new Contacts(from i in Enumerable.Range(0, 20) select new Contact() { ID = i, Key = i, Name = string.Format("Contact{0}", i), PhoneNumber = string.Format("PhoneNumber{0}", i) });
Console.WriteLine("Key for Contact 12: {0}", contacts.GetKey("Contact12", x => x.Name));
Console.WriteLine("Key for PhoneNumber 5: {0}", contacts.GetKey("PhoneNumber5", x => x.PhoneNumber));
Console.ReadLine();
}
}
class Contacts : List<Contact>
{
public Contacts() : base() { ;}
public Contacts(int capacity) : base(capacity) { ;}
public Contacts(IEnumerable<Contact> collection) : base(collection) { ;}
}
class Contact
{
public int ID { get; set; }
public int Key { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
}
static class Extensions
{
public static int GetKey<T>(this IEnumerable<Contact> source, string term, Expression<Func<Contact, T>> selector)
{
int result = -1;
try
{
if (source != null && selector != null)
{
string name = ((MemberExpression)selector.Body).Member.Name;
result = (from item in source
from prop in item.GetType().GetProperties().Where(p => p.Name.Equals(name))
where prop.GetValue(item, null).ToString().Equals(term)
select item.Key).FirstOrDefault();
}
}
catch (Exception)
{
result = -1;
}
return result;
}
}
}
Which produces the following output --saige-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
private int GetKeyByType(string xxx, int type)
{
int findKey = 0;
foreach (var contact in contacts)
{
if(type ==1){
if (contact.Value.Name == xxx)
findKey = contact.Key;}
else if(type == 2){
if (contact.Value.Phone == xxx)
findKey = contact.Key;}
}
.....
return findKey;
}