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

Ganaram inukshuk (talk | contribs)
Ganaram inukshuk (talk | contribs)
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, unless that module is a common library for related templates, or the functions provided are short.
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. A tester function may still be included.
* Modules that serve as libraries for other modules; in such cases, there is no wrapper function other than the tester function.
* 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.
* 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 ====
If a function's expected inputs is determined to be fixed (that is, no new features are expected to ever be added), the underscore-prefixed function can accept them in sequence.<syntaxhighlight lang="lua">
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 a function expects large amounts of input, or if the number of inputs is not known, it should be entered as a table of arguments into the underscore-prefixed function.<syntaxhighlight lang="lua">
</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)