Error Handling
Nil Guard
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
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)
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
user ← Ada, status ← missing
1local user→ Ada = "Ada"2local status→ missing = "missing"status ← ready
4if userAda ~= "" then5 status→ ready = "ready"6endprint("user=" .. user)
8print("user=" .. userAda)9print("status=" .. statusready)outputuser=Ada status=ready
user ← Lin, status ← missing
1local user→ Lin = "Lin"2local status→ missing = "missing"status ← ready
4if userLin ~= "" then5 status→ ready = "ready"6endprint("user=" .. user)
8print("user=" .. userLin)9print("status=" .. statusready)outputuser=Lin status=ready