User:Ganaram inukshuk/Provisional style guide for Lua: Difference between revisions

Ganaram inukshuk (talk | contribs)
No edit summary
Ganaram inukshuk (talk | contribs)
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


-- Function to be called by template
-- 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>These guidelines do not apply for simple modules, or modules that only provide one function.
</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.


Helper functions may be nested within the calling function if those helpers only serve that function. 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)
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>
==== Modules that serve as libraries for other modules ====
Modules that serve to provide functions to other libraries do not need any wrapper functions apart from a tester function.
==== Modules that provide wrappers for more than one template ====
Unless the wrapper functions encapsulate very simple tasks, a module should only ever provide '''one''' wrapper function for '''one''' template.


==== What to pass ====
==== What to pass ====