gpt4 book ai didi

c# - 这个表达式是否需要优化?

转载 作者:行者123 更新时间:2023-12-03 17:10:20 25 4
gpt4 key购买 nike

我不喜欢这个函数的外观。有没有办法让它在没有“魔术弦”的情况下看起来不那么难看。

private static bool Inconsistent(AdStats adStat)   {
return
adStat.Daily.Impressions != adStat.Hourly.Sum(h => h.Value.Impressions) ||
adStat.Daily.Clicks != adStat.Hourly.Sum(h => h.Value.Clicks) ||
adStat.Daily.Spent != adStat.Hourly.Sum(h => h.Value.Spent) ||
adStat.Daily.SocialImpressions != adStat.Hourly.Sum(h => h.Value.SocialImpressions) ||
adStat.Daily.SocialClicks != adStat.Hourly.Sum(h => h.Value.SocialClicks) ||
adStat.Daily.SocialSpent != adStat.Hourly.Sum(h => h.Value.SocialSpent) ||
adStat.Daily.UniqueImpressions != adStat.Hourly.Sum(h => h.Value.UniqueImpressions) ||
adStat.Daily.UniqueClicks != adStat.Hourly.Sum(h => h.Value.UniqueClicks) ||
adStat.Daily.SocialUniqueImpressions != adStat.Hourly.Sum(h => h.Value.SocialUniqueImpressions) ||
adStat.Daily.SocialUniqueClicks != adStat.Hourly.Sum(h => h.Value.SocialUniqueClicks);
}

最佳答案

我认为“优化”是指“减少冗余”又名 Don't Repeat Yourself

本质上,您有一堆指标。您想要检查对于这些指标中的任何指标,广告的该指标的值是否每天 偏离该指标的总和每小时

一旦你这样想,你就可以:

Func<Stat, int>[] metricGetters = 
{
stat => stat.Impressions,
stat => stat.Clicks,
// .. etc. etc.
};

return metricGetters.Any(getter => getter(adStat.Daily)
!= adStat.Hourly.Sum(h => getter(h.Value)));

关于c# - 这个表达式是否需要优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16941418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com