Lua Style Guide: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
m
Text replacement - "</code>" to "</SyntaxHighlight>"
>Trappingnoobs
(→‎Table literals: - Changed '3.1415' to '3.1416', due to '3.14159' rounding up.)
m (Text replacement - "</code>" to "</SyntaxHighlight>")
Line 15: Line 15:
-- end block
-- end block
end
end
</code>
</SyntaxHighlight>


===Expressions===
===Expressions===
Line 23: Line 23:
<dd><code lua>local a = (b + #t)*c
<dd><code lua>local a = (b + #t)*c
local b = -a + 3*(c - a) / 2
local b = -a + 3*(c - a) / 2
print(b == a)</code>
print(b == a)</SyntaxHighlight>
<dt>Wrong:
<dt>Wrong:
<dd><code lua>local a =(b+c)* d
<dd><code lua>local a =(b+c)* d
local b= -a+3*(c-a)/2
local b= -a+3*(c-a)/2
print(b==a)</code>
print(b==a)</SyntaxHighlight>
</dl>
</dl>
===Table Commas===
===Table Commas===
Line 45: Line 45:
3,
3,
}
}
</code>
</SyntaxHighlight>


Not good:
Not good:
Line 52: Line 52:


local data = { 1  , 2,  3,} -- oh god what are you doing
local data = { 1  , 2,  3,} -- oh god what are you doing
</code>
</SyntaxHighlight>


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.
Line 62: Line 62:
7, 8, 9;
7, 8, 9;
}
}
</code>
</SyntaxHighlight>


==Line wrapping==
==Line wrapping==
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)</code>
<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:
Line 75: Line 75:
aPart, anotherPart, 1,
aPart, anotherPart, 1,
5, 8, a, b, c, d
5, 8, a, b, c, d
)</code>
)</SyntaxHighlight>


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


===Nested function calls===
===Nested function calls===
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))</code>
<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
Line 100: Line 100:
)
)
)
)
</code>
</SyntaxHighlight>
or
or


Line 108: Line 108:
                     baz(z, y, x,
                     baz(z, y, x,
                         w, v))
                         w, v))
</code>
</SyntaxHighlight>


===Table literals===
===Table literals===
Line 122: Line 122:
};
};
}
}
</code>
</SyntaxHighlight>


== Semicolons ==
== Semicolons ==
Line 132: Line 132:
print("here"); -- after function calls
print("here"); -- after function calls
return; -- at the end of 'break' or 'return'
return; -- at the end of 'break' or 'return'
</code>
</SyntaxHighlight>


Places to NOT use semicolons:
Places to NOT use semicolons:
Line 140: Line 140:
if (not true) then; end -- after "then"
if (not true) then; end -- after "then"
print("first"; "second"); -- in function calls
print("first"; "second"); -- in function calls
</code>
</SyntaxHighlight>
==See also==
==See also==
*[http://lua-users.org/wiki/LuaStyleGuide Lua Style Guide]
*[http://lua-users.org/wiki/LuaStyleGuide Lua Style Guide]
*[[Whitespace]]
*[[Whitespace]]

Navigation menu