Skip to content
Sarj Code Standards

expression-complexity

Limit Terraform statement and variable-validation expression complexity.

Why

Large predicates hide independent deployment requirements and make an infrastructure change difficult to review.

Fix

Delete redundant checks or name independently meaningful expressions. Preserve lazy evaluation, null guards and dependencies; keep genuine domain validation.

Examples

Before — flagged Fold a complete literal conditional tree
main.tf
locals {
value = true ? (true ? (true ? (true ? (true ? (true ? (true ? (1) : 2) : 2) : 2) : 2) : 2) : 2) : 2
}
After — preferred The selected literal retains its scalar type
main.tf
locals {
value = 1
}
Autofix output
main.tf
locals {
value = 1
}
Before — flagged A statement combines too many independent checks
main.tf
locals {
ready = var.a > 0 && var.b > 0 && var.c > 0 && var.d > 0 && var.e > 0 && var.f > 0 && var.g > 0
}
After — preferred A small range validation remains readable
variables.tf
variable "port" {
type = number
validation {
condition = var.port > 0 && var.port < 65536
error_message = "Supply a valid port."
}
}