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 the book Programming in Lua as well as other documentation. 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, or have suggestions for exercises to add, send us a message in the.

Readings

 * Guidelines:Modules, for a distilled version of the above with Zelda Wiki-specific examples
 * Guidelines:Modules, for a distilled version of the above with Zelda Wiki-specific examples

Setup
  Create the page with the following contents:



 Create the page with the following contents:



 Create the page with the following contents, but don't save the page yet. 

 In the Preview page with this form beneath the main text area, input  in the Page title field and click Show preview. This displays the page as it would appear if you were to save the module changes. You should see the following text on the previewed page:
 * Hello world!



 Scroll past the edit form to the black and green Debug console. In the text field, type the following, then press the Enter key.

You should see the text  appear in the console. 

 Save the module page.  

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 without doing at least a bare minimum of debugging/testing.

Game Links 1
Objective: 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

Setup
  Replace the content of with five invocations, each time with a different canon Zelda game of your choosing. Like so:

The page should now display something like this:
 * Breath of the Wild The Wind Waker Majora's Mask Spirit Tracks Oracle of Ages

</li>

 On the page, put  on line 9. Your  function should look like this:

Preview with these changes. Scroll past the edit box to the very bottom of the page until you see the section Parser profiling data (it may be collapsed by default). At the bottom of that table, uncollapse the the Lua logs section. You should see the following: "Breath of the Wild" "The Wind Waker" "Majora's Mask" "Spirit Tracks" "Oracle of Ages"

allows you to inspect the value assigned to a variable for a given invocation. In this case, each of the five times your module was invoked when loading the preview, the value assigned to the variable  was printed in the logs. They are shown in quotes because they are values of type. </li>

 Move the  call to just before the   line, outside of the   block, and preview the page again:

You should the following logs: nil nil nil nil nil

represents the absence of a value. Because  is declared as a local variable in the   function, it is lexically scoped to that function and has no value outside it. In other words, only the  function "knows" that the variable exists. could declare its own  variable and it would be considered completely different. Likewise, the variable  in our module is unrelated to the   in any other module. </li>

 Leave the edit form without saving. is meant for debugging only and should not be left on a module. </li> </ol>

Problems


 Using concatenation, change the return value to a string like  so that your user sandbox looks like so: </li>
 * Breath of the Wild
 * The Wind Waker
 * Majora's Mask
 * Spirit Tracks
 * Oracle of Ages

 Change the return value to a string like, so that your user sandbox looks like this: </li>
 * Breath of the Wild
 * The Wind Waker
 * Majora's Mask
 * Spirit Tracks
 * Oracle of Ages

 Change the return value to a string like, so that your sandbox looks like this: </li>

 Add  (if you haven't already) or some other game whose full title doesn't start with. Use an statement to ensure the proper link is returned for that game: </li>

 Bonus: Rewrite the function to use instead of concatenation. </li> </ol>

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.

Game Links 2
Objective: Rewrite the template from the previous exercise using Data:Franchise.

Readings

 * Module:Franchise

Game Links 3
Objective: Create a template spec for the template from the previous exercise. Use this spec to document template parameters and validate what editors are providing as input.

Readings

 * Module:Documentation documentation
 * Module:UtilsArg documentation

Game Ratings 1
Objective: Recreate Template:Game Rating from scratch.

Game Ratings 2
Objective: Recreate Module:Game Rating/Data/Documentation.

Game Ratings 3
Objective: Create a data table extension for game rating data.

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 needs to 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