-
Notifications
You must be signed in to change notification settings - Fork 1
Lua Coding Guidelines
campbt edited this page Sep 27, 2015
·
3 revisions
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
- 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
isActiveorhasEntered - Acronyms that are 3 characters or more should only capitalize the first character (if not the first character of the variable). ex:
xmlProcessorForUrl
- Functions should be named similar to variables
- "Getters" and "Setters" should be prefixed with
getVariableName()andsetVariableName(name). Boolean gets should use the is or has prefix like variable names likeisActive()
- Constants should be named with
ALL_CAPSwith underscores to separate words.
- Class names should be
CamelCaseand start with a capital letter. - The
selfkeyword 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)
- 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