User:Ganaram inukshuk/Provisional style guide for Lua: Difference between revisions
No edit summary |
|||
| Line 101: | Line 101: | ||
Templates should consist of at least two functions: a "main" function prefixed with an underscore, and a wrapper function without an underscore. | Templates should consist of at least two functions: a "main" function prefixed with an underscore, and a wrapper function without an underscore. | ||
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. | 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. A module should only provide '''one''' wrapper for '''one''' template, unless that module is a common library for related templates, or the functions provided are short. | ||
For testing purposes, a tester function may be added, which is itself a wrapper that calls the main function.<syntaxhighlight lang="lua"> | For testing purposes, a tester function may be added, which is itself a wrapper that calls the main function.<syntaxhighlight lang="lua"> | ||
| Line 109: | Line 109: | ||
end | end | ||
-- | -- Wrapper function; to be called by template | ||
function p.call_me(frame) | function p.call_me(frame) | ||
return p._call_me(frame.args) | return p._call_me(frame.args) | ||
| Line 119: | Line 119: | ||
return p._call_me(args) | return p._call_me(args) | ||
end | end | ||
</syntaxhighlight> | </syntaxhighlight>The guidelines stated above do not apply to the following: | ||
* Modules that serve as libraries for other modules. A tester function may still be included. | |||
* Modules whose functions are short, or modules that only provide one function. For such modules used by its corresponding template, the code for the "main" function may be combined with the wrapper function. | |||
==== Helper functions ==== | ==== Helper functions ==== | ||
For code readability and code reusability within the module, the use of helper functions is recommended. | For code readability and code reusability within the module, the use of helper functions is recommended. | ||
Ideally, helper functions should be nested within the calling function if those helpers only serve that function, but this rule may be disregarded for testing purposes. Nested functions have access to the variables and parameters of the outer function, so an equivalent nested function may require fewer parameters.<syntaxhighlight lang="lua">function some_function(args) | |||
-- Get args here | -- Get args here | ||
local arg1 = args["Arg 1"] | local arg1 = args["Arg 1"] | ||
| Line 134: | Line 137: | ||
end | end | ||
end</syntaxhighlight> | end</syntaxhighlight> | ||
==== What to pass ==== | ==== What to pass ==== | ||