Lua

Lesson 01

Tables as records and arrays

Keep table shape clear when a value behaves like a record, array, or map.

Good Code

src/review_summary.lua
local function build_review_summary(title, score)
  -- Named fields make this table read like a stable record.
  return {
    title = title,
    score = score,
    approved = score >= 4,
  }
end

Bad Code

review_summary.lua
local function build_review_summary(title, score)
  -- Positional fields hide what each slot means to the caller.
  return { title, score, score >= 4 }
end

Review Notes

What to review

Good Code

The good version uses named fields, so callers can read the table as a record and access values without remembering index positions.

Bad Code

The bad version mixes unrelated values into positional slots. A caller has to know that index 3 means approval state, which is easy to break during later edits.

Takeaways

  • Lua table review should name whether a table is a record, array, map, or mixed structure.