Lua Style Guide: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
m
Text replacement - "<code lua>" to "<SyntaxHighlight code="lua">"
m (Text replacement - "</code>" to "</SyntaxHighlight>")
m (Text replacement - "<code lua>" to "<SyntaxHighlight code="lua">")
Line 4: Line 4:
Each level of block should be indented by a single tab.
Each level of block should be indented by a single tab.


<code lua>
<SyntaxHighlight code="lua">
local var = 10
local var = 10
if var > 0 do
if var > 0 do
Line 21: Line 21:
<dl>
<dl>
<dt>Right:
<dt>Right:
<dd><code lua>local a = (b + #t)*c
<dd><SyntaxHighlight code="lua">local a = (b + #t)*c
local b = -a + 3*(c - a) / 2
local b = -a + 3*(c - a) / 2
print(b == a)</SyntaxHighlight>
print(b == a)</SyntaxHighlight>
<dt>Wrong:
<dt>Wrong:
<dd><code lua>local a =(b+c)* d
<dd><SyntaxHighlight code="lua">local a =(b+c)* d
local b= -a+3*(c-a)/2
local b= -a+3*(c-a)/2
print(b==a)</SyntaxHighlight>
print(b==a)</SyntaxHighlight>
Line 33: Line 33:


Good:
Good:
<code lua>
<SyntaxHighlight code="lua">
local data = {1,2,3}
local data = {1,2,3}


Line 48: Line 48:


Not good:
Not good:
<code lua>
<SyntaxHighlight code="lua">
local data = {1 ,2 ,3}
local data = {1 ,2 ,3}


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


<code lua>
<SyntaxHighlight code="lua">
local data = {
local data = {
1, 2, 3;
1, 2, 3;
Line 68: Line 68:
Say you have this function call, and you want to line-wrap it at the 1.
Say you have this function call, and you want to line-wrap it at the 1.


<code lua>local return = foo(aPart, anotherPart, 1, 5, 8, a, b, c, d)</SyntaxHighlight>
<SyntaxHighlight code="lua">local return = foo(aPart, anotherPart, 1, 5, 8, a, b, c, d)</SyntaxHighlight>


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:
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:


<code lua>local return = foo(
<SyntaxHighlight code="lua">local return = foo(
aPart, anotherPart, 1,
aPart, anotherPart, 1,
5, 8, a, b, c, d
5, 8, a, b, c, d
Line 79: Line 79:
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:
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:


<code lua>local return = foo(aPart, anotherPart, 1,
<SyntaxHighlight code="lua">local return = foo(aPart, anotherPart, 1,
                     5, 8, a, b, c, d)</SyntaxHighlight>
                     5, 8, a, b, c, d)</SyntaxHighlight>


Line 85: Line 85:
The following function call
The following function call


<code lua>local return = foo(bar(a, b, c, d, e, f), baz(z, y, x, w, v))</SyntaxHighlight>
<SyntaxHighlight code="lua">local return = foo(bar(a, b, c, d, e, f), baz(z, y, x, w, v))</SyntaxHighlight>


Could be line wrapped to one of
Could be line wrapped to one of


<code lua>
<SyntaxHighlight code="lua">
local return = foo(
local return = foo(
bar(
bar(
Line 103: Line 103:
or
or


<code lua>
<SyntaxHighlight code="lua">
local return = foo(bar(a, b, c,
local return = foo(bar(a, b, c,
                         d, e, f),
                         d, e, f),
Line 113: Line 113:
Treat these in the same way as function calls.
Treat these in the same way as function calls.


<code lua>
<SyntaxHighlight code="lua">
local my_table = {
local my_table = {
a = "potato";
a = "potato";
Line 128: Line 128:


Places to use semicolons:
Places to use semicolons:
<code lua>
<SyntaxHighlight code="lua">
local assignment = "value"; -- after assignments
local assignment = "value"; -- after assignments
print("here"); -- after function calls
print("here"); -- after function calls
Line 135: Line 135:


Places to NOT use semicolons:
Places to NOT use semicolons:
<code lua>
<SyntaxHighlight code="lua">
for i = 1; 10 do end -- in loops
for i = 1; 10 do end -- in loops
do; end -- after "do", this includes loops
do; end -- after "do", this includes loops

Navigation menu