Should I compare Boolean values with == true or == false?
← Comparison Patterns · Ref: Q639
EK9 detects redundant Boolean comparisons and raises E08083. Comparing a Boolean to a literal (true or false) is always redundant because the Boolean IS the condition.
THE PROBLEM
'if ready == true' is equivalent to 'if ready'. The '== true' adds nothing. Similarly, 'if ready == false' is equivalent to 'if not ready' (using Boolean negation). The extra comparison is dead code.
CORRECT PATTERNS
Direct use: 'if isReady' instead of 'if isReady == true'
Negation: 'if not isReady' instead of 'if isReady == false'
Combined: 'if isReady and isValid' instead of 'if isReady == true and isValid == true'
WHY THIS IS FLAGGED
Redundant Boolean comparisons indicate unclear thinking about Boolean logic. They also create opportunities for copy-paste errors: 'if x == true and y == true' could accidentally become 'if x == true and x == true' (self-comparison plus redundant comparison).
BOOLEAN OPERATORS
Use Boolean operators directly: and, or, not, xor. These express intent clearly without comparing to literals.
See Q637 for self-comparison detection. See Q638 for named constant patterns. See Q558 for tautological conditions.
Example
defines module qa.comparison.booleanuse defines function <?- Correct: Boolean value used directly in condition. Adding '== true' would trigger E08083. -?> checkEligibility() as pure -> isAdult as Boolean hasConsent as Boolean <- eligible as Boolean: isAdult and hasConsent <?- Correct: Boolean negation using 'not' operator. Writing 'isBlocked == false' would trigger E08083. -?> canProceed() as pure -> isBlocked as Boolean isReady as Boolean <- proceed as Boolean: not isBlocked and isReady <?- Correct: combines multiple Boolean values directly. No comparison to literals needed. -?> classifyAccess() as pure -> isAuthenticated as Boolean isAuthorized as Boolean isActive as Boolean <- accessLevel as String: "denied" if isAuthenticated and isAuthorized and isActive accessLevel: "full" else if isAuthenticated and isActive accessLevel: "limited" else if isAuthenticated accessLevel: "readonly" <?- Correct: Boolean return from comparison, used directly. -?> isInRange() as pure -> testValue as Integer minBound as Integer maxBound as Integer <- inRange as Boolean: testValue >= minBound and testValue <= maxBound defines program BooleanWithoutLiteralsDemo() stdout <- Stdout() //Use variables for Boolean arguments adult <- true consented <- true eligible <- checkEligibility(isAdult: adult, hasConsent: consented) stdout.println(`Eligible: ${eligible}`) blocked <- false ready <- true proceed <- canProceed(isBlocked: blocked, isReady: ready) stdout.println(`Can proceed: ${proceed}`) authenticated <- true authorized <- true active <- true lvl <- classifyAccess(isAuthenticated: authenticated, isAuthorized: authorized, isActive: active) stdout.println(`Access: ${lvl}`) inRange <- isInRange(testValue: 5, minBound: 1, maxBound: 10) stdout.println(`In range: ${inRange}`)
Common mistakes
E08084 — Comparing a Boolean to the literal true is redundant. The Boolean IS the condition. Use it directly. See ek9 -h E08084 for details.
Incorrect:
if isAuthenticated == true and isAuthorized == true and isActive == true
Correct:
if isAuthenticated and isAuthorized and isActive
E08084 — Comparing Booleans to literal values is redundant. Use not for negation and the Boolean directly for truth. See ek9 -h E08084 for details.
Incorrect:
proceed as Boolean: isBlocked == false and isReady == true
Correct:
proceed as Boolean: not isBlocked and isReady
Other ways to ask this
- What is E08083 redundant Boolean comparison?
- Why does EK9 reject 'if flag == true'?
- How should I use Boolean values in conditions?
Coming from another language?
Java: no detection of redundant boolean comparison. SonarQube flags 'if (x == true)' as minor code smell. Rust: clippy has bool_comparison lint (warn). Go: no detection. Python: pylint has C0121 singleton-comparison. Kotlin: detects some redundant boolean comparisons. EK9: mandatory compile error E08083 for comparing Boolean to literal.
Keywords: direct, condition, literal, redundant, comparison, true, boolean, false, E08084, E08083, pattern