User:Ganaram inukshuk/Provisional style guide for Lua: Difference between revisions
| Line 99: | Line 99: | ||
===== "Main", wrapper, and tester functions ===== | ===== "Main", wrapper, and tester functions ===== | ||
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 of the same name but 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. A module should only provide '''one''' wrapper for '''one''' 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. | ||
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. This allows it to be tested in the in-browser console by calling mw.logObject(p.tester()). Tester functions may be removed if the "main" function is determined to be functional under expected conditions.<syntaxhighlight lang="lua">-- "Main" function to be called by wrapper or another module | ||
-- "Main" function to be called by wrapper or another module | |||
function p._call_me(args) | function p._call_me(args) | ||
return "something" .. args["something"] | return "something" .. args["something"] | ||
| Line 118: | Line 117: | ||
local args = { ["something"] = 123 } | local args = { ["something"] = 123 } | ||
return p._call_me(args) | return p._call_me(args) | ||
end | end</syntaxhighlight>The guidelines stated above do not apply to the following: | ||
</syntaxhighlight>The guidelines stated above do not apply to the following: | |||
* Modules that serve as libraries for other modules | * Modules that serve as libraries for other modules; in such cases, there is no wrapper function other than the tester function. | ||
* Modules | * Modules that provide wrappers to several related templates; in such cases, the rule regarding one wrapper for one template does not apply. | ||
* Modules that provide only one function for other modules; in such cases, no wrapper functions are necessary. | |||
* Template-based modules whose wrapper function calls another module's functions; in such cases, no other functions are necessary. | |||
==== Helper functions ==== | ==== Helper functions ==== | ||
| Line 138: | Line 138: | ||
end</syntaxhighlight> | end</syntaxhighlight> | ||
==== What to pass ==== | ==== What to pass into a "main" function ==== | ||
For an underscore-prefixed "main" function, if its expected inputs is determined to be fixed (for example, no new features are expected to ever be added), the inputs may be passed in one-by-one.<syntaxhighlight lang="lua"> | |||
-- "Main" function to be called by wrapper or another module | -- "Main" function to be called by wrapper or another module | ||
function p._call_me(arg1, arg2, arg3) | function p._call_me(arg1, arg2, arg3) | ||
-- Code goes here | -- Code goes here | ||
end | end | ||
</syntaxhighlight>If | </syntaxhighlight>If there is large amounts of input, or if the number of inputs is not known, it should be entered as a table of arguments.<syntaxhighlight lang="lua"> | ||
-- "Main" function to be called by wrapper or another module | -- "Main" function to be called by wrapper or another module | ||
function p._call_me(args) | function p._call_me(args) | ||