User:Ganaram inukshuk/Provisional style guide for Lua: Difference between revisions
| Line 130: | Line 130: | ||
return table.concat(lines, '\n') | return table.concat(lines, '\n') | ||
</syntaxhighlight> | |||
=== Use of nested functions === | |||
Recommended, but may be disregarded for testing. Functions in a function have access to all local variables declared before it, so an equivalent nested function may require fewer parameters.<syntaxhighlight lang="lua"> | |||
function some_function(args) | |||
-- Get args here | |||
local arg1 = args["Arg 1"] | |||
local arg2 = args["Arg 2"] | |||
-- Helper function | |||
local helper_function(some_value) | |||
end | |||
end | |||
</syntaxhighlight> | |||
=== Use of wrapper functions === | |||
Recommended, but may be disregarded for simple modules. The use of a wrapper and "main" function allows for a module to be used directly in another module or indirectly through its corresponding template. This also allows for code testing by calling the "main" function, or through a tester function.<syntaxhighlight lang="lua"> | |||
-- "Main" function to be called by wrapper or another module | |||
function p._call_me(args) | |||
end | |||
-- Function to be called by template | |||
function p._call_me(frame) | |||
local args = getArgs(frame) | |||
return p._call_me(args) | |||
end | |||
-- Tester function; test wrapper that calls the "main" function | |||
function p.tester() | |||
local args = {} | |||
return p._call_me(args) | |||
end | |||
</syntaxhighlight> | </syntaxhighlight> | ||