Guidelines:Modules/Exercises

The following exercises are designed to help editors learn to make contributions in the Module namespace. The only prerequisite is an understanding of how templates are used.

The exercises have assigned readings from Programming in Lua. The readings explain the concepts covered in the exercise. Read them once before starting the exercise and refer back to them if you get stuck.

If you have any questions or feedback about these exercises, please bring them to the.

Recap
This exercise covered:


 * How articles use modules.
 * Your sandbox page uses a template but has no notion of modules per se. The template uses the  parser function to invoke the function   on your module. The process is the same for real modules. The Link page uses Template:Term which invokes a function on Module:Term.


 * How modules use template arguments.
 * Your sandbox page passes the argument  to the template. The module retrieves that argument from the   object. (More on that later.)


 * How to make sure your module works before saving.
 * The preview form and the debug console are the two main tools for debugging modules. Never submit code blindly.

Game Links 1
The goal of this exercise is to recreate the functionality of game link templates using Lua.

Readings

 * Local Variables and Blocks (until third paragraph)
 * Types and Values
 * Strings
 * Concatenation
 * Logical Operators
 * if then else

Recap
This exercise covered:


 * Variables and lexical scope.
 * Local variables only have meaning in the block (function, if statement, for loop, etc.) or chunk (module) in which they are declared. Attempting to use one outside its scope produces . This is a good thing, as it prevents other parts of the code from changing data unintentionally.
 * Global variables are almost always a bad idea.


 * Data types.
 * The data types relevant to Scribunto are,  ,   ,  ,  , and  . At a given point in time, a variable will have any one of these types. A variable's type can change when it it is re-assigned—Lua is a dynamically typed language like JavaScript or Python, as opposed to a statically typed one like C or Java.


 * String manipulation
 * Most modules boil down to being string manipulators. They take as input some string(s) and output some other string (usually wikitext).


 * Control flow
 * This exercise involved using  statements with the relational operator  . There are other relational operators such as   and , as well as the logical operators  ,  , and.

Solutions
Solutions for the above exercises may be found at Module:Exercises.

Though there are many ways to solve each problem, some solutions are better than others in terms of programming style. When writing real modules, take the time to think about what you can do to make the code easier to understand for the next person who has to read it or change it.

"Programs must be written for people to read, and only incidentally for machines to execute."

- Gerald Jay Sussman and Hal Abelson, Structure and Interpretation of Computer Programs