An order can be in the “status” of Completed, Corrected or some other status.
I saw some code that is checking it like this, the purpose is to disable some stuff when the status is in Completed or Corrected status.
if (model.CurrentStatus != DSRHelper.OrderStatusEnum.Complete && model.CurrentStatus != DSRHelper.OrderStatusEnum.Corrected)
I can’t get it why the engineer has used “AND” for this, shouldn’t it be an “OR”?
3
No, AND appears to be correct here. If you switched it to OR, the condition would always be true. If it was Completed, then the second condition would be true. If it was Corrected, then the first would be true.
Keep in mind that the condition currently used is equivalent to
!(model.CurrentStatus == DSRHelper.OrderStatusEnum.Complete || model.CurrentStatus == DSRHelper.OrderStatusEnum.Corrected)
No, the AND
is correct: if you put OR
, you’d get a condition that is always true
, because the status cannot be Complete
and Corrected
at the same time.
To understand why, use De Morgan’s Laws to convert your proposed condition to an equivalent. You start with
NOT(status is complete) OR NOT(status is corrected)
and convert to
NOT((status is complete) AND (status is corrected))
this is always false
.
If the condition used OR, the condition would always be true. In English, the current condition says “If the Current Status is not Complete and is not Corrected then…” implying that any state but those two will cause the condition to be true.
If you used OR, then absolutely any state would cause the condition to be true. If the current status is Complete, then the first half of the condition will be false, but the second half will be true, so the condition is true. If the current status is Corrected, the first half of the condition will be true so without even checking the second half, the entire condition is true (some languages or will still check the second half, but the condition will still be true if they do). If you use OR and the current status is neither Complete or Corrected then both halves of the condition are true. So OR will always be true.