LunaLua style guide

From Moondust Wiki
Revision as of 23:54, 27 March 2018 by The0x539 (talk | contribs)
Jump to navigation Jump to search

This document covers mostly code formatting, rather than programming practices.

Whitespace

function foo(x, y) -- Spaces between arguments
	-- Indent with tabs. Comments should match indentation.
	local myVar    = 1 --Optionally align. If aligning, use spaces, in case tab width/behavior differs for someone else.
	local otherVar = 2

	local someTbl = {
		"Tables split across",
		"multiple lines should",
		"look like this."
	}
	print(#someTbl) --No spaces after unary operators.
	return x + myVar --Spaces between binary operators.
end

Syntax Elements

local x = "Single-line strings should use double quotes."
-- Semicolons are discouraged.
-- If using semicolons in a code file, use consistently throughout entire file.
print("Use parentheses when calling functions using string literals.")
-- Should the require function become common in LunaLua, it is a possible exception to the above rule.
x = doSomething{ --No space here
	instruction = "Do not use parentheses",
	condition = "when calling functions using table literals."
}
if simpleCondition then
	-- No parentheses needed for simple conditions
end
if (usingParentheses) then
	-- Put space on either side of condition
end

Comments

--------------------
-- SECTION HEADER --
--------------------

-- Prose comments on their own line should have a space after the --.
local x --This is optional for end-of-line comments, though these should always have space before the --.
-- Multi-line comments should generally still use line comment syntax

--- LDoc-compatible function description.
-- Optional additional description lines.
-- @param foo first parameter
-- @param bar second parameter
function doAThing(foo, bar)
	return foo + bar
end

LunaLua

Library layout

  • Table declaration
  • Load dependencies
  • Private/exposed variables
  • Private/exposed functions
    • Private variables/functions going first is preferred.
  • onInitAPI event handler
  • Other event handlers, ordered by event loop ordering (first global, then per-NPC, for each event)
  • Return statement