Guard checks prevent invalid values from reaching code that expects real data.

guard before failing A simple guard often keeps error handling local and easier to explain.

Nil Guard

user
nil_guard.lua
Replay: real traced execution (multi-file project)
local user = ""
local status = "missing"

if user ~= "" then
  status = "ready"
end

print("user=" .. user)
print("status=" .. status)
local user = "Ada"
local status = "missing"

if user ~= "" then
  status = "ready"
end

print("user=" .. user)
print("status=" .. status)
local user = "Lin"
local status = "missing"

if user ~= "" then
  status = "ready"
end

print("user=" .. user)
print("status=" .. status)
  1. user ← (empty), status ← missing

    1local user→ (empty) = "" --@user="Ada", "Lin"2local status→ missing = "missing"34if user ~= "" then5  status = "ready"6end78print("user=" .. user(empty))9print("status=" .. statusmissing)
    outputuser=
    status=missing
  1. user ← Ada, status ← missing

    1local user→ Ada = "Ada"2local status→ missing = "missing"
  2. status ← ready

    4if userAda ~= "" then5  status→ ready = "ready"6end
  3. print("user=" .. user)

    8print("user=" .. userAda)9print("status=" .. statusready)
    outputuser=Ada
    status=ready
  1. user ← Lin, status ← missing

    1local user→ Lin = "Lin"2local status→ missing = "missing"
  2. status ← ready

    4if userLin ~= "" then5  status→ ready = "ready"6end
  3. print("user=" .. user)

    8print("user=" .. userLin)9print("status=" .. statusready)
    outputuser=Lin
    status=ready