Is it right to deny the whole if statement to check for null values?

I am a little novice programmer. What I'm trying to do here is to check if there is a time, if it is selected, and if this time is equivalent to another. If this is all true, I want to skip the code block below it. Here is a sample code:

if (currentGVR.Round_Start_Time)
{
     if (tr.StartLunchDateTime != null && currentGVR.Round_Start_Lunch && roundedStart == roundedStartL)
           // skip
     else
     {
           key = tr.TransactionID;
           TransactionRecords[key]["Start_DateTime"] = roundedStart;
     }
}

I was thinking about using the OR operator, but I can see where the error occurred if there was no time to compare. Using the AND operator avoids this dilemma.

Thus, the general question is whether coding correctly cancels all conditions in order to get the correct result, for example. if (! (cond)), and also, would this be the best way to check if there is a value to compare before actually comparing it in C # and otherwise? In some entries, the time may be zero (or not exist). Any recommendations?

+4
source share
3 answers

I would cancel all of these conditions and switched &&on ||, so that it more quickly detect the conditions under which the code will (or will not) perform.

Plus (in my experience), you usually don't see an empty ifblock with all the code under else.

if (tr.StartLunchDateTime == null || !currentGVR.Round_Start_Lunch || roundedStart != roundedStartL)
{
    key = tr.TransactionID;
    TransactionRecords[key]["Start_DateTime"] = roundedStart;
}
+6
source

Statement

 if (tr.StartLunchDateTime != null && currentGVR.Round_Start_Lunch && roundedStart == roundedStartL){
       // skip
 }
 else
 {
       key = tr.TransactionID;
       TransactionRecords[key]["Start_DateTime"] = roundedStart;
 }

equivalently

 if (!(tr.StartLunchDateTime != null && currentGVR.Round_Start_Lunch && roundedStart == roundedStartL))
 {
       key = tr.TransactionID;
       TransactionRecords[key]["Start_DateTime"] = roundedStart;
 }
 else {
       // skip
 }

This can be further simplified since

!(tr.StartLunchDateTime != null && 
  currentGVR.Round_Start_Lunch &&
  roundedStart == roundedStartL)

,

(!(tr.StartLunchDateTime != null) ||
  !(currentGVR.Round_Start_Lunch) ||
  !(roundedStart == roundedStartL))

(tr.StartLunchDateTime == null ||
 !currentGVR.Round_Start_Lunch ||
 roundedStart != roundedStartL)

. DeMorgan.

+3
if (someLongCondition)
{ }
else
{
    doStuff();
}

:

if (!someLongCondition)
{
    doStuff();
}

So yes, you can simply deny all your condition:

if (!(tr.StartLunchDateTime != null && currentGVR.Round_Start_Lunch && roundedStart == roundedStartL))
{ … }

But you can also pull negation (applying De Morgan's laws ) and write it like this:

if (tr.StartLunchDateTime == null || !currentGVR.Round_Start_Lunch || roundedStart != roundedStartL)
{ … }

They are all equivalent, so choose what makes the condition more understandable (in fact, consider storing it in a separate variable that you give a descriptive name).

0
source

Source: https://habr.com/ru/post/1588877/


All Articles