skip to content

A review finds the guard isActive AND (isActive OR hasOverride); why does it collapse to isActive alone?

level: middleimportance: should knowfreq 46%

answer

  1. one clause cannot change the outcome
  2. case-split on the repeated atom
  3. a subsumed disjunct can be deleted
  4. absorption, not merely idempotence
  5. both mentions must be the same value

basics

~10 s

Absorption. When isActive is false the whole conjunction is false; when it is true the inner disjunction is already true, so the outcome is isActive either way. The hasOverride clause cannot affect the result.

solid answer

~50 s

This is the **absorption** law: `a AND (a OR b)` is equivalent to `a`, and dually `a OR (a AND b)` is equivalent to `a`. Check it by cases on `isActive`. If it is false, the outer AND is false regardless of the rest. If it is true, then `isActive OR hasOverride` is true because `isActive` is one of its disjuncts, and `true AND true` is true. So the guard equals `isActive` on every input, and `hasOverride` is dead weight — it never changes an answer. The related law is **idempotence**, `a AND a` is `a`, which is what you fall back to when the duplicate sits at the same level. The one caveat is that both occurrences of `isActive` must denote the same value; if the atom re-reads mutable state, the two mentions are not the same proposition and the algebra no longer describes the program.

go deeper

for a junior

Learn the case-split rather than the law's name. Try the repeated atom as true, then as false, and see that the other clause never changes the result. The name absorption can come later.

for a middle

Name absorption and idempotence, derive one from distribution plus subsumption, and say why the collapsed form is the honest one: the dead clause advertises behaviour the code does not have.

for a senior

Add the condition under which the algebra applies: both mentions of the atom must denote one value. An atom that re-reads mutable state or a clock must be evaluated once into a local name before you simplify.

for a principal

Treat it as a specification question, not a cleanup. Absorption proves the guard ignores overrides entirely, so the team has to decide whether the code or the intent is wrong before anyone edits the expression.

## The shape and what it hides Duplicated clauses arrive in a guard the way most complexity does: a condition is extended, then extended again by someone who did not want to disturb the existing test. The result in the access filter reads `isActive AND (isActive OR hasOverride)`, and to a reader it looks like there are two ways in — being active, or having an override. There are not. `hasOverride` is inert. Case-split on `isActive`, which is the whole proof: 1. `isActive` is **false**. The outer AND is false immediately; nothing else is consulted. 2. `isActive` is **true**. Then `isActive OR hasOverride` is true, because `isActive` is itself one of the disjuncts. So the outer AND is `true AND true`, which is true. On both branches the guard's value equals `isActive`, so the two expressions are equivalent — and the reader who thought the override mattered was misled by the text. ## The laws that do this work | law | form | collapses to | |---|---|---| | idempotence | `a AND a`, `a OR a` | `a` | | absorption | `a AND (a OR b)`, `a OR (a AND b)` | `a` | | distribution | `a AND (b OR c)` | `(a AND b) OR (a AND c)` | | complement | `a AND NOT a`, `a OR NOT a` | `false`, `true` | Absorption is the one that matters in review because it is the one people do not recognise on sight. Idempotence is obvious — a repeated test at the same level reads as a typo. Absorption hides behind a change of connective, and the duplicated atom sits one nesting level away from its twin, which is exactly far enough for the eye to skip it. Distribution is in the table because it is how absorption is often *derived* rather than seen: expand `isActive AND (isActive OR hasOverride)` into `(isActive AND isActive) OR (isActive AND hasOverride)`, apply idempotence to the first term to get `isActive`, and notice the second term is a strengthening of the first — anything satisfying `isActive AND hasOverride` already satisfies `isActive`. A disjunct that is subsumed by another disjunct can be deleted, which leaves `isActive`. The two routes give the same answer, and the derivation is worth knowing because the subsumption step generalises to guards where no textbook law matches exactly. ## Why this is more than tidying - A dead clause is a **false signal to the reader**: it advertises a behaviour ("an override can get you in") the code does not have. - It is also a false signal to the *writer of the next change*. Someone asked to make overrides work will fix the override implementation, see the clause already present, and ship nothing. - Dead clauses attract tests. A test asserting "override grants access when inactive" would fail against this guard, and the usual reaction is to weaken the test rather than to notice the guard is wrong. - Collapsed guards are cheaper to negate. Every extra clause doubles the work of the De Morgan rewrite that a deny-first version of the filter needs. The honest conclusion of this review is usually **not** "delete `hasOverride`". It is "the guard does not do what its text implies — decide which you meant". If overrides really should grant access to an inactive principal, the intended condition was `isActive OR hasOverride`, and the bug is the outer AND. ## Where the algebra stops describing the program Boolean algebra reasons about **truth values**, and it assumes that the two mentions of `a` are two mentions of one value. Programs can break that assumption: - the atom re-reads mutable shared state that another worker can change between the two evaluations; - the atom consults a clock, a counter, or a remote party, and can legitimately answer differently the second time; - the atom has a side effect that the surviving expression would no longer perform. If any of those hold, evaluate the atom once into a local name and simplify the names. Then the algebra is describing the same object the program is. One worry that does **not** apply here is the loss of an evaluation of `b`. Under short-circuiting operators, `hasOverride` is never evaluated in either absorption shape: in `a AND (a OR b)`, a false `a` short-circuits the outer AND, and a true `a` short-circuits the inner OR before reaching `b`. The same trace holds for `a OR (a AND b)`. So removing the clause removes no work that was ever done — it only removes the misleading text.

  • Does removing the hasOverride clause change how much work the guard does at runtime?
    No. Under short-circuiting operators `hasOverride` was never evaluated: a false `isActive` short-circuits the outer AND, and a true `isActive` satisfies the inner OR before its right operand is reached. The rewrite removes text, not computation — which is also why nobody noticed the clause was dead.
  • The team says the override was meant to work. What is the actual fix?
    The intended condition was almost certainly `isActive OR hasOverride`, so the defect is the outer AND, not the duplication. Say that explicitly in review: absorption proves the current guard ignores overrides, which converts a style note into a behaviour bug with a decision attached.
  • How would you show a sceptical reviewer that `a OR (a AND b)` is also just `a`?
    Case-split the same way. If `a` is true the outer OR is true immediately. If `a` is false then `a AND b` is false, so the OR is false. Both branches return `a`, independent of `b`. It is the dual of the first absorption form, with AND and OR exchanged.

saying these in an interview costs you the question

  • The result still depends on hasOverride, so the clause must stay
  • Repeating a clause makes the guard stricter
  • The two forms differ on some inputs, so the rewrite is risky
  • The two mentions of isActive can be treated as independent unknowns
  • Idempotence means a AND a is stronger than a
  • Simplifying is only cosmetic and changes nothing a reader cares about