gpt4 book ai didi

c# - 编写具有良好 C# 语法的简单 bool 方法的最佳方法

转载 作者:行者123 更新时间:2023-11-30 19:07:29 26 4
gpt4 key购买 nike

大家好,我好像到处都有这种方法。

下面的方法想要完成这些简单的任务:

  1. 打开数据库连接(IntLMPDB 对象)
  2. 从一个小的数据库表中读取一条简单的记录(数据库表以字符串作为键,因为每个字符串都是方法的名称,所以我只需要表中的每一行),该行的其余部分是一系列时间戳,告诉我事情何时发生。
  3. 如果找不到记录,则返回异常,因为无能为力。
  4. 如果您找到记录,请查看第二个日期(如果它只是明显缺失),然后设置为 true,因为这是有史以来第一次运行。
  5. 或者最后进入正题,如果第一个日期大于第二个日期,则将其设置为 True,因为它已经更新并且该运行了。否则,如果没有,则设置为 False,因为还没有更新。

所以这是代码...我正在尝试做的是将其缩减为运行这些检查的最佳和最快的方法。我不关心数据库连接问题或类似问题。

    private static bool isLastIntervalNewerThanDB(string muiMethod)
{
using (var db = new IntLMPDB())
{
// Try to load a matching record.
LastIntervalUpdated liRec = db.LastIntervalUpdateds.FirstOrDefault(rec => rec.method == muiMethod);
// If it could not be loaded, exit because there's no way to determine if we should run.
if (liRec == null) { throw new Exception(string.Format("Could NOT find LastIntervalUpdated record for muiMethod: {0}", muiMethod)); }
else
{
// we have a valid interval record, so lets check that it has been updated since the last webPostTime.
// put another way, there are three datetime values in the LastIntervalUpdated table. First is the
// interval itself, second is the retrievalTime and third is the webPostTime. Whenever the MUI is
// checked for a new interval if one is found then the code updates the retrievalTime to the current
// instant in time. This tells us what the last interval this application found on its last run was.
// The thrid value is the webPostTime, this time instant is only updated by this very method we're in
// right here. We can use this logic: if the retrievalTime is greater than the webPostTime then there's
// a newer interval that we haven't yet processed and inserted into the databse. So we should run the
// method below and update ALL of the syncable values into the databse. Then we'll set the dbPostTime to
// the current instance. As it goes, if this program then runs again before the interval is updated
// then the dbPostTime will be greater than the retrieval time and we'll know to do nothig. Simple Right? :)

// or check here includes a NULL check on dbPostTime because it's possible that dbPostTime is NULL,
// in the example of the first time the system runs. It might have run a LastUpdate sync and not yet
// done this method, so dbPostTime would be NULL. None of the other columns are allowed to be null.
if (liRec.dbPostTime_EST == null || liRec.retrievalTime_EST > liRec.dbPostTime_EST)
{ return true; }
else { return false; }
}
}
}

最佳答案

我觉得逻辑没问题。我建议你做这样的事情来提高可读性*:

private static bool isLastIntervalNewerThanDB(string muiMethod)
{
using (var db = new IntLMPDB())
{
LastIntervalUpdated liRec
= db.LastIntervalUpdateds
.FirstOrDefault(rec => rec.method == muiMethod);

if (liRec == null)
{
throw new Exception(
string.Format("Could NOT find LastIntervalUpdated record for muiMethod: {0}", muiMethod));
}

return liRec.dbPostTime_EST == null
|| liRec.retrievalTime_EST > liRec.dbPostTime_EST;
}
}

*虽然可读性是主观的,但我认为 if (liRec == null) 之后的 else 只会添加不必要的嵌套并且可以折叠最终条件成一个单一的表达。也永远不要低估长表达式中位置恰当的换行符 - 它可以使代码的可读性和不可读性完全不同。

关于c# - 编写具有良好 C# 语法的简单 bool 方法的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5095738/

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