compared with
Current by Peter Romianowski
on Sep 03, 2008 07:55.


 
Key
These lines were removed. This word was removed.
These lines were added. This word was added.

View page history


There are 3 changes. View first change.

 h2. LUA
  
 h3. Syntax
  
 * All names are written using CamelHumps. So it is {{myCoolMethod}} and not {{my_cool_method}}.
 * Use braces where possible.
  
 {code:title=Common patterns}
 local withBraces = require("withBraces")
  
 if (termA == termB) then
  someFunction()
 end
 {code}
  
 h3. Visibility
  
 * Always use the smallest visibility.
 * *Never* use the global scope.
 * Thus all variables and functions must be declared as {{local}}. This is checked by a {{lint}} (i.e. code analysis) task.
  
 h3. Variables
  
 * Member variables which are local to a module or a class begin with an underscore.
  
 h3. Modules and classes
  
 * A file can only contain *one* module or class.
 * Usage of the {{module()}} function is forbidden. See http://lua-users.org/wiki/LuaModuleFunctionCritiqued
  
 {code:title=Example of a module}
 local _module = {}
  
 local _myModuleLocalVariable = "Invisible to users"
 _module.myPublicVariable = "Visibile to users"
  
 function _module.someFunction()
  _myModuleLocalVariable = "Some secret stuff."
  _module.myPublicVariable = "Open to everyone"
 ...
 end
  
 return _module
 {code}
  
 {code:title=Example of a class}
  
 local MyClass = {}
 MyClass.__index = MyClass
  
 -- Since a file can only contain a single class it is the only thing we export.
 _module = MyClass
  
 -- Static function.
 MyClass.staticFunction()
 end
  
 -- Instance creation.
 MyClass.create()
  local self = {}
  setmetatable(self, Query)
  
  self._memberVariable = "member"
  
  return self
 end
  
 MyClass:instanceFunction()
  self._memberVariable = "realMember"
 end
  
 -- Private functions begin with two underscores.
 MyClass:__privateFunction()
 end
  
 return _module
 {code}
  
 h3. Imports / require
  
 * Imports have to be assigned to local variables.
 * Classes are assigned to a variable beginning with an uppercase character.
  
 {code:title=Example import}
local myClass = require("myClass")
  local MyClass = require("myClass")
 local myNormalModule = require("myNormalModule")
  
 -- Create an instance.
 local myInstance = myClass.create()
  local myInstance = MyClass.create()
 {code}