if statement - Best way to write a simple boolean method with good C# syntax -


hey all, seem have these types of methods everywhere.

the method below wants these simple tasks:

  1. open db connection (intlmpdb object)
  2. read simple record off small db table (the db table keyed strings, because each string name of method, want 1 row of each in table), rest of row series of timestamps tell me when things happned.
  3. if can't find record, return exception because there's nothing can done.
  4. if find record, @ second date if it's plain missing, set true because first ever run.
  5. or meat, if first date greater second set true because it's been updated , it's time run. else if not, set false because there's no update yet.

so here's code... trying pare down best , quickest way run through these checks. not concerned db connection issues or that.

    private static bool islastintervalnewerthandb(string muimethod)     {        using (var db = new intlmpdb())         {             // try load matching record.             lastintervalupdated lirec = db.lastintervalupdateds.firstordefault(rec => rec.method == muimethod);             // if not loaded, exit because there's no way determine if should run.              if (lirec == null) { throw new exception(string.format("could not find lastintervalupdated record muimethod: {0}", muimethod)); }             else             {                 // have valid interval record, lets check has been updated since last webposttime.                 // put way, there 3 datetime values in lastintervalupdated table. first                 // interval itself, second retrievaltime , third webposttime. whenever mui                  // checked new interval if 1 found code updates retrievaltime current                 // instant in time. tells last interval application found on last run was.                 // thrid value webposttime, time instant updated method we're in                 // right here. can use logic: if retrievaltime greater webposttime there's                 // newer interval haven't yet processed , inserted databse. should run                 // method below , update of syncable values databse. we'll set dbposttime                  // current instance. goes, if program runs again before interval updated                 // dbposttime greater retrieval time , we'll know nothig. simple right? :)                  // or check here includes null check on dbposttime because it's possible dbposttime null,                 // in example of first time system runs. might have run lastupdate sync , not yet                 // done method, dbposttime null. none of other columns allowed null.                 if (lirec.dbposttime_est == null || lirec.retrievaltime_est > lirec.dbposttime_est)                 { return true; }                 else { return false; }             }        }     } 

i think logic fine. suggest improve readability*:

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 muimethod: {0}", muimethod));          }          return lirec.dbposttime_est == null              || lirec.retrievaltime_est > lirec.dbposttime_est;            } } 

*while readability subjective argue else after if (lirec == null) adds unnecessary nesting , final conditional can collapsed single expression. never underestimate well-placed newline within long expression - can make difference betwee readable , unreadable code.


Comments

Popular posts from this blog

Javascript line number mapping -

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -