避免此代码重复的最佳方法是什么

发布于 2024-11-27 02:25:57 字数 556 浏览 0 评论 0原文

我有以下方法:

    public  int CountProperty1
    {
        get
        {
            int count = 0;
            foreach (var row in Data)
            {
                count = count + row.Property1;
            }
            return count ;
        }
    }

    public  int CountProperty2
    {
        get
        {
            int count = 0;
            foreach (var row in Data)
            {
                count = count + row.Property2;
            }
            return count ;
        }
    }

避免重复并共享尽可能多的代码的最佳方法是什么

i have the following methods:

    public  int CountProperty1
    {
        get
        {
            int count = 0;
            foreach (var row in Data)
            {
                count = count + row.Property1;
            }
            return count ;
        }
    }

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

嘦怹 2024-12-04 02:25:57

使用 LINQ 和 Sum 扩展方法怎么样?

public int CountProperty1 
{
    get { return Data.Sum(r => r.Property1); }
} 

如果这不是一个选项,您可以将逻辑重构为您自己的 sum 方法:

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 ;
}

请注意最后一个示例:我编写了“Row”类型,因为从您的示例中并不明显。用正确的类型替换。

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.

孤凫 2024-12-04 02:25:57

可能不是您正在寻找的答案,但这不一定是重复。有时存在一种误解,如果两个不同的函数看起来相同,则应该重构它们以删除重复项。以我的拙见,这是错误的。

只有当它们是真正的复制并且相同或接近相同的概念时,才算是重复。对于重复(至少应该删除重复),不仅仅是它们碰巧使用相同的代码,而是出于相同的原因使用相同的代码。

这可能只是因为您发布的示例,但它很简单。尽管这两个属性的实现是相同的,但必须有一些有效的业务原因才能拥有这两个属性,否则最简单的答案就是一起删除第二个属性。但是,如果您确实有两个属性,并且它们现在恰好看起来相同,这并不意味着它们将来在功能上不会出现差异(这是可以的)。

这个想法是为了最小化复杂性和维护成本。只有当您的代码有意义并模拟现实时,您才能做到这一点,但如果它通过将恰好看起来相似的事物混在一起而引入错误的比较,则不能这样做。

http://mooneyblog.mmdbsolutions.com/ index.php/2010/07/30/reusable-code-is-bad/

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/

够钟 2024-12-04 02:25:57

别打扰。

您可以缩短代码,但如果您尝试重构它以消除重复,则只会使代码变得更加复杂。

将重构工作集中在更复杂、更有价值的案例上。生命太短暂...

几个字节的磁盘空间非常便宜,而复制/粘贴是开发人员最好的朋友。另外,如果我们对此很纯粹,请考虑重构的运行时开销。

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.

凉城 2024-12-04 02:25:57

我个人不会打扰属性,只需使用 linq 直接在外部调用

myObject.Data.Sum(x=>x.Property1)

I wouldn't bother with properties personally, just invoke is directly outside using linq

myObject.Data.Sum(x=>x.Property1)
苦行僧 2024-12-04 02:25:57

这是相当干净的。如果我知道行的类型,我会倾向于将 PropertyN() 放入其类中而不是放在这里,但缺乏这些知识:

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;
}

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;
}
白龙吟 2024-12-04 02:25:57

给它一个输入参数,说明哪个参数

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 ;
        }
    }

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 ;
        }
    }
倾听心声的旋律 2024-12-04 02:25:57
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 ;
}

在 pribvate 方法中,我在签名中使用了对象 - 直到我对属性所需的类型有了更好的了解

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文