Skip to content

Lesson 9 of 9 · 24m

Writing Clean, Testable, Operable Lua

Structure Lua modules for clarity, isolate dependencies, test behavior with real harnesses, and operate scripts with versioning, linting, and observability.

The worst Lua codebase I inherited was not ugly because Lua encourages ugliness. It was ugly because every script was treated as a temporary glue patch: globals everywhere, hidden dependencies, no tests, no ownership, and deployment by editing files on a server. Lua's flexibility is a tool. Clean Lua comes from imposing the discipline the language politely refuses to impose for you.

Modules should be explicit

A module returns behavior rather than mutating global scope:

local M = {}
function M.price_key(sku, currency)
    return sku:upper():gsub("%s+", "") .. ":" .. currency
end
return M

Consumers write local pricing = require("pricing"). Globals are accidental coupling: they make reloads dangerous, tests brittle, and collisions inevitable.

Dependency injection beats hidden reach

Clean Lua functions receive context instead of reaching into the universe:

function M.can_refund(order, policy, clock)
    if order.status ~= "captured" then return false, "order not captured" end
    return clock.now() - order.captured_at <= policy.refund_window_seconds
end

Time and policy are inputs. Hidden calls to os.time or global config turn every unit test into an environment reconstruction exercise.

Lint before review

Run luacheck lua/ spec/ and configure the runtime truth in .luacheckrc. OpenResty, Roblox, LuaJIT, Lua 5.4, and embedded hosts expose different globals; copying one lint configuration across them is not governance.

Test behavior at the right layer

busted spec runs the common Lua test harness. A focused policy test controls dependencies explicitly:

local refund = require("refund")
describe("refund policy", function()
    it("rejects uncaptured orders", function()
        local ok = refund.can_refund({status="pending", captured_at=100},
            {refund_window_seconds=3600}, {now=function() return 200 end})
        assert.is_false(ok)
    end)
end)

Unit tests belong around pure policy. Integration tests belong around nginx phases, Roblox services, C callbacks, and event schedulers.

Operate scripts like production code

Version scripts, pin dependencies, use a repeatable package process, and expose script versions in telemetry. LuaRocks can package reusable modules, but deployment discipline matters more than packaging format. Keep module boundaries narrow enough that replacement and deletion remain plausible; a module that knows HTTP, storage, UI, metrics, and deployment is a gravity well, not glue.

Field notes

  • require caches modules per Lua state; hot reload behavior depends on the runtime, not just the file contents.
  • Circle dependencies are easier to create in dynamic languages and harder to untangle after deployment.
  • Passing a clock or randomness source into policy functions makes tests deterministic.
  • Lint rules should encode the runtime truth: Roblox, LuaJIT/OpenResty, Lua 5.4, and embedded hosts are not interchangeable environments.

Was this lesson helpful?

Sign in to rate