Back to Opa

boolean-assignment

docs/projects/regal/rules/idiomatic/boolean-assignment.md

1.16.11.5 KB
Original Source

boolean-assignment

Summary: Prefer if over boolean assignment

Category: Idiomatic

Avoid

rego
package policy

more_than_one_member := count(input.members) > 1

Prefer

rego
package policy

more_than_one_member if count(input.members) > 1

Rationale

Assigning the result of a boolean function is almost always redundant, as the boolean value returned by the expression rarely is used for anything but to determine whether to continue evaluation. Moving the condition to the body following an if will have the rule either evaluate to true or be undefined. For the few cases where false should be returned, using a default rule assignment is preferable, as it is guaranteed to be assigned a value even on undefined input:

rego
package policy

default more_than_one_member := false

# will be assigned `false` even if input.members is undefined
more_than_one_member if count(input.members) > 1

Configuration Options

This linter rule provides the following configuration options:

yaml
rules:
  idiomatic:
    boolean-assignment:
      # one of "error", "warning", "ignore"
      level: error