有没有办法删除这段代码中的重复项

发布于 2024-08-30 08:32:59 字数 790 浏览 6 评论 0原文

我有一个如下所示的方法:

   private double GetX()
    {
        if (Servings.Count > 0)
        {
            return Servings[0].X;
        }
        if (!string.IsNullOrEmpty(Description))
        {
            FoodDescriptionParser parser = new FoodDescriptionParser();
            return parser.Parse(Description).X;
        }
        return 0;
    }

我有另一个如下所示的方法:

  private double GetY()
    {
        if (Servings.Count > 0)
        {
            return Servings[0].Y;
        }
        if (!string.IsNullOrEmpty(Description))
        {
            FoodDescriptionParser parser = new FoodDescriptionParser();
            return parser.Parse(Description).Y;
        }
        return 0;
    }

有什么方法可以巩固这个,因为唯一不同的是属性名称?

i have a method that looks like this:

   private double GetX()
    {
        if (Servings.Count > 0)
        {
            return Servings[0].X;
        }
        if (!string.IsNullOrEmpty(Description))
        {
            FoodDescriptionParser parser = new FoodDescriptionParser();
            return parser.Parse(Description).X;
        }
        return 0;
    }

and i have another method that looks like this:

  private double GetY()
    {
        if (Servings.Count > 0)
        {
            return Servings[0].Y;
        }
        if (!string.IsNullOrEmpty(Description))
        {
            FoodDescriptionParser parser = new FoodDescriptionParser();
            return parser.Parse(Description).Y;
        }
        return 0;
    }

Is there any way to consolidate this as the only thing different is the property names?

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

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

发布评论

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

评论(3

不可一世的女人 2024-09-06 08:32:59

创建一个单独的 GetServing 方法:

private Serving GetServing() {
    if (Servings.Count > 0)
        return Servings[0];

    if (!string.IsNullOrEmpty(Description)) {
        FoodDescriptionParser parser = new FoodDescriptionParser();
        return parser.Parse(Description);
    }
    return null;
}

private double GetX() {
    Serving serving = GetServing();
    if (serving == null) return 0;
    return serving.X;
}

private double GetY() {
    Serving serving = GetServing();
    if (serving == null) return 0;
    return serving.Y;
}

Make a separate GetServing method:

private Serving GetServing() {
    if (Servings.Count > 0)
        return Servings[0];

    if (!string.IsNullOrEmpty(Description)) {
        FoodDescriptionParser parser = new FoodDescriptionParser();
        return parser.Parse(Description);
    }
    return null;
}

private double GetX() {
    Serving serving = GetServing();
    if (serving == null) return 0;
    return serving.X;
}

private double GetY() {
    Serving serving = GetServing();
    if (serving == null) return 0;
    return serving.Y;
}
素衣风尘叹 2024-09-06 08:32:59
private double Get(Func<SomeType, double> valueProvider)
{
    if (Servings.Count > 0)
    {
        return valueProvider(Servings[0]);
    }
    if (!string.IsNullOrEmpty(Description))
    {
        FoodDescriptionParser parser = new FoodDescriptionParser();
        return valueProvider(parser.Parse(Description));
    }
    return 0;
}

可以这样使用:

var x = Get(value => value.X);
var y = Get(value => value.Y);

备注: SomeTypeServings[0] 的类型,如果我正确理解你的代码,它应该与 的类型相同parser.Parse(描述)

private double Get(Func<SomeType, double> valueProvider)
{
    if (Servings.Count > 0)
    {
        return valueProvider(Servings[0]);
    }
    if (!string.IsNullOrEmpty(Description))
    {
        FoodDescriptionParser parser = new FoodDescriptionParser();
        return valueProvider(parser.Parse(Description));
    }
    return 0;
}

Which could be used like this:

var x = Get(value => value.X);
var y = Get(value => value.Y);

Remark: SomeType is the type of Servings[0] which if I understand your code correctly should be the same as the type of parser.Parse(Description).

薔薇婲 2024-09-06 08:32:59

假设 parser.Parse() 返回与 Servings[] 保存的类相同,您可以创建一个 该类型的空对象,X 和Y 为零。然后,您可以有一个函数返回 Servings[] 的第一个元素(如果存在),或者返回 new FoodDescriptionParser.Parser(Description)(如果 Description) code> 存在,或者最后是那个空对象。并根据需要收集 X 或 Y。

Assuming parser.Parse() returns the same class that Servings[] holds, you could create a null object of that type, for which both X & Y are zero. Then you could have a function that returns the first element of Servings[], if it exists, or new FoodDescriptionParser.Parser(Description), if Description exists, or, finally, that null object. And gather the X or Y as needed.

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