Lua Style Guide

From Legacy Roblox Wiki
Jump to navigationJump to search

Roblox Lua Style guide

The official style guide can be read at Roblox Lua style guide. Note the following when applying it to clients in the scope of this wiki:

  • You may place a "wait for child" function between the script-level constants and the script-level variables.
  • The information about requires is not applicable, because these clients have no practical use for it.
  • if-then-else expressions are not supported. You will have to use the x and y or z pattern instead.
  • The information about Roact is not applicable, because these clients have no support for it.

NXTBoy's Lua Style guide

This section about NXTBoy's style guide, which is not official.

These rules do not need to be followed, are not arbitrary and should in no way be considered as a reference. These rules are only presentational, and so do not have to be followed for code to work, but they might make code easier to read, write, and debug.

Whitespace Rules

Indentation

Each level of block should be indented by a single tab.

local var = 10
if var > 0 do
	-- new block
	for i = 1, var do
		-- new block
		print(i)
		-- end block
	end
	-- end block
end

Expressions

Spaces are optional around * and /. If there is one after, always put one before, and vice versa. Always include spaces before and after =, and all the other binary operators. Unary operators, such as -a and #a should not have a space between the operator and the operand.

Right:
local a = (b + #t)*c
local b = -a + 3*(c - a) / 2
print(b == a)
Wrong:
local a =(b+c)* d
local b= -a+3*(c-a)/2
print(b==a)

Table Commas

Never put a space before a comma, but multiple spaces may come after. Make sure spacing is consistent. If the table is put on a single line, the comma after the last entry can usually be omitted.

Good:

local data = {1,2,3}

local data = {1, 2, 3}

local data = { 1, 2, 3 }

local data = {
	1,
	2,
	3,
}

Not good:

local data = {1 ,2 ,3}

local data = {	1  , 2,   3,} -- oh god what are you doing

Since tables accept either commas or semi-colons as separators, use semi-colons at the end of a line instead of commas.

local data = {
	1, 2, 3;
	4, 5, 6;
	7, 8, 9;
}

Line wrapping

Function calls

Say you have this function call, and you want to line-wrap it at the 1.

local return = foo(aPart, anotherPart, 1, 5, 8, a, b, c, d)

The first option is to bring all the arguments to the next line, indent them one level (with a tab), and then wrap them. The close parenthesis should go on the following line:

local return = foo(
	aPart, anotherPart, 1,
	5, 8, a, b, c, d
)

The second option is to align the arguments by column. Note that this option, however, will be misinterpreted by many programming editors. This should be done with spaces, not tabs:

local return = foo(aPart, anotherPart, 1,
                    5, 8, a, b, c, d)

Nested function calls

The following function call

local return = foo(bar(a, b, c, d, e, f), baz(z, y, x, w, v))

Could be line wrapped to one of

local return = foo(
	bar(
		a, b, c,
		d, e, f
	),
	baz(
		z, y, x,
		w, v
	)
)

or

local return = foo(bar(a, b, c,
                        d, e, f),
                    baz(z, y, x,
                        w, v))

Table literals

Treat these in the same way as function calls.

local my_table = {
	a = "potato";
	b = "Taco!";
	c = 3.1416;
	new = {
		reaction = "yay!";
	};
}

Semicolons

Besides seperating values in a table, semicolons (;) are used to denote the end of a statement. They are not required, but can solve some Whitespace Ambiguities.

Places to use semicolons:

local assignment = "value";	-- after assignments
print("here");	-- after function calls
return;	-- at the end of 'break' or 'return'

Places to NOT use semicolons:

for i = 1; 10 do end -- in loops
do; end	-- after "do", this includes loops
if (not true) then; end	-- after "then"
print("first"; "second");	-- in function calls

See also