Skip to content

Lua Coding Guidelines

campbt edited this page Sep 27, 2015 · 3 revisions

Lua Coding Guidelines

This page will describe the coding guidelines we'd like to enforce in the SWAT project. If writing Lua scripts, please follow these conventions as closely as you can.

This is a good resource overall, but it is a bit ambiguous so use the specific definitions defined here.
http://lua-users.org/wiki/LuaStyleGuide

Variables

  • Use local variables whenever possible
  • Variable names should be decently descriptive of what their purpose is (avoid single character variables).
  • Variables should be camelCasedLikeThis
  • Boolean variables should begin with a prefix like isActive or hasEntered
  • Acronyms that are 3 characters or more should only capitalize the first character (if not the first character of the variable). ex: xmlProcessorForUrl

Functions

  • Functions should be named similar to variables
  • "Getters" and "Setters" should be prefixed with getVariableName() and setVariableName(name). Boolean gets should use the is or has prefix like variable names like isActive()

Constants

  • Constants should be named with ALL_CAPS with underscores to separate words.

Classes

  • Class names should be CamelCase and start with a capital letter.
  • The self keyword should be used for methods that refer to the class instance
  • Use the : operator for method declarations and method calling.
  • Add comments to the top of all non trivial methods and to the class itself.
  • Use new() for the constructor name. Instantiate objects with var = SomeClass:new(arg1, arg2)

Indentation

  • Every block needs to be indented one more than the enclosing block. Keep the keywords defining the block at the lower indentation level.
    Example:
    if not isActive then
    ⋅⋅⋅⋅while isReading do
    ⋅⋅⋅⋅⋅⋅⋅⋅...
    ⋅⋅⋅⋅end
    elseif isReading then
    ⋅⋅⋅⋅...
    else
    ⋅⋅⋅⋅...
    end

Clone this wiki locally