Practical Scripting Patterns
Config Defaults
Choose a configuration value with a clear fallback.
config-default
Small scripts often combine one explicit setting with a safe default. Keeping the selected value in a scalar variable makes the decision easy to inspect.
Config Defaults
config_default.lua
Replay: real traced execution (multi-file project)
local mode = "prod"
local port = 80
if mode == "dev" then
port = 8080
elseif mode == "test" then
port = 9000
end
print("mode=" .. mode)
print("port=" .. port)
local mode = "dev"
local port = 80
if mode == "dev" then
port = 8080
elseif mode == "test" then
port = 9000
end
print("mode=" .. mode)
print("port=" .. port)
local mode = "test"
local port = 80
if mode == "dev" then
port = 8080
elseif mode == "test" then
port = 9000
end
print("mode=" .. mode)
print("port=" .. port)
mode ← prod, port ← 80
1local mode→ prod = "prod" --@mode="dev", "test"2local port→ 80 = 8034if mode == "dev" then5 port = 80806elseif mode == "test" then7 port = 90008end910print("mode=" .. modeprod)11print("port=" .. port80)outputmode=prod port=80
mode ← dev, port ← 80
1local mode→ dev = "dev"2local port→ 80 = 80port ← 8080
4if modedev == "dev" then5 port→ 8080 = 80806elseif mode == "test" thenprint("mode=" .. mode)
10print("mode=" .. modedev)11print("port=" .. port8080)outputmode=dev port=8080
mode ← test, port ← 80
1local mode→ test = "test"2local port→ 80 = 80port ← 9000
5 port = 80806elseif modetest == "test" then7 port→ 9000 = 90008endprint("mode=" .. mode)
10print("mode=" .. modetest)11print("port=" .. port9000)outputmode=test port=9000