Error Handling
Nil Guard
Guard checks prevent invalid values from reaching code that expects real data.
Nil Guard
nil_guard.lua
local user =
local status = "missing"
if user ~= "" then
status = "ready"
end
print("user=" .. user)
print("status=" .. status)
local user =
local status = "missing"
if user ~= "" then
status = "ready"
end
print("user=" .. user)
print("status=" .. status)
local user =
local status = "missing"
if user ~= "" then
status = "ready"
end
print("user=" .. user)
print("status=" .. status)
guard before failing
A simple guard often keeps error handling local and easier to explain.