编写具有良好C#语法的简单布尔方法的最佳方法

Pau*_*uer 2 c# if-statement

嘿所有,我似乎到处都有这些类型的方法.

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

  1. 打开数据库连接(IntLMPDB对象)
  2. 从一个小的DB表中读取一个简单的记录(DB表是用字符串键入的,因为每个字符串都是方法的名称,所以我只希望表中每行一行),其余部分是一系列时间戳告诉我什么时候开心.
  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; }
            }
       }
    }
Run Code Online (Sandbox Code Playgroud)

And*_*are 10

我认为逻辑很好.我建议你做这样的事情来提高可读性*:

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;        
   }
}
Run Code Online (Sandbox Code Playgroud)

*尽管可读性是主观的我认为一个elseif (liRec == null)仅增加了不必要的嵌套和最终条件可以折叠成一个单一的表达.也永远不要低估长表达式中良好放置的换行符 - 它可以使可读和不可读代码之间的所有区别.