Lua

Lesson 03

Module return contracts

Return an explicit module table so require calls expose a stable API instead of hidden globals.

Good Code

src/review_rules.lua
local ReviewRules = {}

function ReviewRules.is_approved(review)
  -- The exported function is visible on the returned module table.
  return review.score ~= nil and review.score >= 4
end

return ReviewRules

Bad Code

review_rules.lua
function is_approved(review)
  -- Global function names can collide across unrelated modules.
  return review.score >= 4
end

Review Notes

What to review

Good Code

The good version builds and returns a module table. A caller using require can see the exported surface and keep names scoped.

Bad Code

The bad version writes a global function during load. Another file can replace it, and tests can pass or fail depending on import order.

Takeaways

  • A Lua module should return the functions it wants callers to use and avoid mutating global state during import.