Skip to main content

W0069 - No And/Or

Warning

test_and(A, B) ->
A and B.
%% ^^^^^^^ 💡 warning: W0069: Use `andalso` instead of `and`. OTP 29 will warn for `and` usage.

Explanation

Starting from OTP 29, the Erlang/OTP compiler will warn for uses of the and and or operators. This warning helps you migrate your code ahead of time.

Semantic Differences

The and/or operators have different semantics from andalso/orelse:

  • and/or: Evaluate both operands before applying the boolean operation
  • andalso/orelse: Short-circuit operators that may not evaluate the right operand if the result can be determined from the left operand alone

Why This Matters

  1. OTP 29 Compatibility: The compiler will start warning about and/or usage
  2. Exception Handling: andalso/orelse won't evaluate the right operand if unnecessary, avoiding potential exceptions
  3. Performance: Short-circuit evaluation can be more efficient when the right operand is expensive to compute

Simple Fix

For simple cases where short-circuit evaluation is acceptable:

% Before
test_and(A, B) -> A and B.
test_or(A, B) -> A or B.

% After
test_and(A, B) -> A andalso B.
test_or(A, B) -> A orelse B.

When Both Operands Must Be Evaluated

If you need to ensure both operands are evaluated (e.g., for side effects like logging or validation), bind them to variables first:

% Before - both validate calls execute, all errors logged
validate_all(X, Y) ->
validate(X) and validate(Y).

% After - preserve the behavior of evaluating both sides
validate_all(X, Y) ->
R1 = validate(X),
R2 = validate(Y),
R1 andalso R2.

This pattern ensures:

  • All side effects occur (both validate calls run)
  • The result is still a boolean AND of both results
  • OTP 29 compatibility

See Also