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

Ganaram inukshuk (talk | contribs)
Ganaram inukshuk (talk | contribs)
Line 103: Line 103:
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. 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
A tester function may be added for testing purposes, which is itself a wrapper that also calls the "main" function. This allows it to be tested using 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
function p._call_me(args)
function p._call_me(args)
     return "something" .. args["something"]
     return "something" .. args["something"]
Line 119: Line 119:
end</syntaxhighlight>The guidelines stated above do not apply to the following:
end</syntaxhighlight>The guidelines stated above do not apply to the following:


* Modules that serve as libraries for other modules; in such cases, there is no wrapper function other than the tester function.
* Modules that serve as libraries for other modules; in such cases, there is no wrapper function other than the tester function. Example: [[Module:MOS]] and [[Module:Rational]]
* 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 multiple functions, including wrapper functions, meant to be invoked by multiple templates; in such cases, the rule regarding one wrapper for one template does not apply. Example: [[Module:Rational]] and [[Module:Utils]]
* Modules that provide only one function for other modules; in such cases, no wrapper functions are necessary.
* Modules that provide only one function for other modules; in such cases, no wrapper functions are necessary. Example: [[Module:Yesno]]
* Template-based modules whose wrapper function calls another module's functions; in such cases, no other functions are necessary.
* Template-based modules where writing a separate "main" function produces a short function; in such cases, the code for that function may be merged with the wrapper function. Example: [[Module:MOS scalesig]]
* Template-based modules whose wrapper function calls another module's functions; in such cases, no other functions are necessary. Example: [[Module:MOS scalesig]]


==== Helper functions ====
==== Helper functions ====
Line 139: Line 140:


==== What to pass into a "main" function ====
==== 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">
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 function may accept them individually.<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 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">
</syntaxhighlight>If there is large amounts of input, or if the number of inputs is not known, the function should have a single parameter that is a table of passed-in 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)