Help:Modules

From Path of Exile Wiki
Jump to navigation Jump to search

Lua is a programming language that is available, via the Scribunto MediaWiki extension. Lua code can now be embedded into wiki templates by employing the "{{#invoke:}}" functionality of Scribunto. The Lua source is stored in pages called modules (e.g., Module:Skill). These individual modules are then invoked (by code {{#invoke:}}) on template page. Modules are used when more advanced functions with high performance are required.

Writing modules

General

  • Use underscore_naming for variables and functions
  • Use 4 spaces for indentation
  • Add extension documentation to /doc if possible, however this is not required if the code is readable
  • If implementing a template in lua
    • Add to the lua comments which template(s) are implemented by the function
    • Add to the /doc page which templates are implemented
    • Add to a link from the template's /doc page using Template:Lua

Module documenation

  • Add appropriate categories to the template (NOT the /doc subpage, i.e. use <includeonly></includeonly> tags)

Coding style

Define variables as local unless they are otherwise needed

As the title suggest try to define locally in the scope they are needed.

Compress boolean statements if possible

This may be require some basic familiarity with Boolean algebra.

  • Examples
    -- Bad
    not (a == b)
    -- Good
    a ~= b
    
    -- Bad
    not (a ~= b)
    -- Good
    a == b
    
    -- Bad
    a and (a or b)
    -- Good
    a
    
    -- Bad
    a or (a and b)
    -- Good
    a
    

Strings connecting

There are 3 basic rules:

  • If the string is very long to connect, use a table and table.concat. This is faster and more readable
    -- Don't
    local out = ''
    out = out .. 'a'
    out = out .. 'b'
    out = out .. 'c'
    out = out .. 'd'
    out = out .. 'e'
    out = out .. 'f'
    return out
    -- Do
    local out = {}
    out[#out+1] = 'a'
    out[#out+1] = 'b'
    out[#out+1] = 'c'
    out[#out+1] = 'd'
    out[#out+1] = 'e'
    out[#out+1] = 'f'
    return table.concat(out, '')
    
  • If the string is short, but has multiple arguments, use string.format, while it may be a bit slower, it is much more readable
    -- Hard to read
    "#ask:[[Is event::" .. args.type .. "]] [[Has release date::<" .. args.release_date .. "]]"
    -- Better to read
    string.format("#ask:[[Is event::%s]] [[Has release date::<%s]]", args.type, args.release_date)
    
  • Use .. for other simple cases
    -- This is ok
    myvar .. other_var .. whatever
    

Avoid code duplication

Please try to avoid any form of repeated (in particular 'spaghetti') code. Minor repeats where required are, but large portions of the code repeating itself should be avoided.

Consider the following techniques:

  • Use library modules to reduce code duplication
  • Use poe wiki library modules like Module:Util, Module:Game to reduce code duplication
  • Move code that is used regularly into a function; if the code is used...:
    • ... only used in that module, add as local, non callable function to the module
    • ... used primarily in that module, but may be used in others, add it to the return table
    • ... is generally useful for a lot of various modules consider adding it to Module:Util (discuss on talk page first)
  • For formatting strings based on conditions, consider moving the formatting itself outside of the conditions if it is the same
  • For long if ... elseif ... elseif ... elseif ... end that execute similar code create a table and loop trough the table

For editors

For other modules

See also

ru:Path of Exile Wiki:Обзор модулей