Good Code
The good version turns optional input into one normalized table. Later code can rely on limit and include_archived having predictable types.
Lesson 02
Normalize nil at the boundary so the rest of the code can distinguish missing data from empty values.
local function normalize_options(options)
options = options or {}
-- Defaults are chosen once before the review job runs.
return {
limit = options.limit or 20,
include_archived = options.include_archived == true,
}
endlocal function fetch_reviews(options)
-- Every read repeats a nil decision with slightly different rules.
local limit = options and options.limit or 20
local archived = options and options.include_archived
return query_reviews(limit, archived)
endThe good version turns optional input into one normalized table. Later code can rely on limit and include_archived having predictable types.
The bad version repeats nil checks inside the workflow. Each line can drift into a different default rule, especially when booleans are involved.