Good Code
The good version exposes a narrow command and validates script input before changing host state.
Lesson 08
Keep host-provided APIs small and explicit when Lua scripts extend an application.
local api = {}
function api.add_note(review, text)
-- The host API validates script input before mutating review state.
assert(type(text) == "string", "note text must be a string")
review.notes[#review.notes + 1] = text
end
return apilocal api = {}
function api.expose_review(review)
-- Scripts receive the full mutable host object.
return review
end
return apiThe good version exposes a narrow command and validates script input before changing host state.
The bad version hands the whole host object to scripts. A script can mutate fields the host does not expect, making failures hard to trace.