what is the best way to avoid duplication on this code
i have the following methods:
public int CountProperty1
{
get
{
int count = 0;
foreach (var row in Data)
{
count = count + row.Property1;
}
return count ;
开发者_JS百科 }
}
public int CountProperty2
{
get
{
int count = 0;
foreach (var row in Data)
{
count = count + row.Property2;
}
return count ;
}
}
what is the best way to avoid duplication here and share as much code as possible
How about using LINQ and the Sum extension method ?
public int CountProperty1
{
get { return Data.Sum(r => r.Property1); }
}
In case that is not an option, you can refactor out the logic into your own sum method:
public int CountProperty1
{
get
{
return CountProperty(r => r.Property1);
}
}
public int CountProperty2
{
get
{
return CountProperty(r => r.Property2);
}
}
private int CountProperty(Func<Row,int> countSelector)
{
int count = 0;
foreach (var row in Data)
{
count = count + countSelector(row);
}
return count ;
}
Note about the last example: I made up the "Row" type, as it was not evident from your example. Substitute with the proper type.
Probably not the answer you are looking for, but this is not necessarily duplication. There is a misunderstanding sometimes that if two different functions happen look the same, they should be refactored to remove the duplicate. In my humble opinion, THIS IS WRONG.
It is only dupliction if they are truly replication and identicial or near-identical concept. For it to be duplication (at least duplication that should be removed), it's not just that they happen to use the same code, but that the use the same code for the same reason.
It may just be because of the sample you posted, but it was simple enough. Despite the fact that the implemention of the two properties was identical, there must be some valid business reason that you have those two properties, otherwise the simplest answer is to remove the second property all together. However, if you really have two properties, and they just happen to look the same now, that doesn't mean they they won't diverge in functionility in the future (and that is OK).
The idea is to minimize complexity and maintenence cost. You can only do that if your code makes sense and models reality, but not if it introduces false comparisons by lumping together things that just happen to look similar.
http://mooneyblog.mmdbsolutions.com/index.php/2010/07/30/reusable-code-is-bad/
Don't bother.
You can shorten the code, but if you try to refactor it to eliminate the duplication, you're just going to make that code more complicated.
Concentrate your refactoring effort on more complicated and worthy cases. Life's too short...
A few bytes of disk space are cheap in the extreme and Copy/Paste are the developer's best friend. Also, if we're being purist about it, consider the runtime overhead of your refactoring.
I wouldn't bother with properties personally, just invoke is directly outside using linq
myObject.Data.Sum(x=>x.Property1)
This is fairly clean. If I knew the type of row, I'd be inclined to put PropertyN() into its class rather than here, but lacking that knowledge:
public int CountPropertyN(int n)
{
int count = 0;
foreach (var row in Data)
{
count = count + PropertyN(row, n)
}
return count ;
}
private int PropertyN(var row, int n)
{
if (n == 1) return row.Property1;
else return row.Property2;
}
give it an input parameter that says which parameter
public int CountProperty (int whichProperty)
{
get
{
int count = 0;
foreach (var row in Data)
{
if( whichProperty = 1)
count = count + row.Property1;
if( whichProperty = 2)
count = count + row.Property2;
}
return count ;
}
}
public int CountProperty1
{
get
{
return GetCount(row.Property1);
}
}
public int CountProperty2
{
get
{
return GetCount(row.Property2);
}
}
private int GetCount(object property)
{
int count = 0;
foreach (var row in Data)
{
if(property == row.Property1)
{
count = count + row.Property1;
}
else if (property == row.Property2)
{
count = count + row.Property2;
}
}
return count ;
}
in the pribvate method i used object in the signature - until i have a better knowelege of the type needed for property
精彩评论