Lua Style Guide: Difference between revisions

Making two sections, one for the official style guide, and one for the NXTBoy style guide
(Linking to official style guide)
(Making two sections, one for the official style guide, and one for the NXTBoy style guide)
Line 1: Line 1:
{{Hatnote|This article is about NXTBoy's style guide. For the official style guide, see [https://roblox.github.io/lua-style-guide/ Roblox Lua style guide].}}
== Roblox Lua Style guide ==
The official style guide can be read at [https://roblox.github.io/lua-style-guide/ Roblox Lua style guide]. Note the following when applying it to [[Help:Contributing|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.
* <code>if-then-else</code> expressions are not supported. You will have to use the <code>x and y or z</code> pattern instead.
== NXTBoy's Lua Style guide ==
{{Hatnote|This section about NXTBoy's style guide, which is not official.}}
These rules do not need to be followed, are not arbitrary and <strong>should in no way be considered as a reference</strong>. These rules are only presentational, and so do not have to be followed for code to work, but they <em>might</em> make code easier to read, write, and debug.
These rules do not need to be followed, are not arbitrary and <strong>should in no way be considered as a reference</strong>. These rules are only presentational, and so do not have to be followed for code to work, but they <em>might</em> make code easier to read, write, and debug.
==Whitespace Rules==
=== Whitespace Rules ===
===Indentation===
==== Indentation ====
Each level of block should be indented by a single tab.
Each level of block should be indented by a single tab.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local var = 10
local var = 10
Line 17: Line 22:
end
end
</syntaxhighlight>
</syntaxhighlight>
 
==== Expressions ====
===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.
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.
<dl>
<dl>
Line 30: Line 34:
print(b==a)</syntaxhighlight>
print(b==a)</syntaxhighlight>
</dl>
</dl>
===Table Commas===
==== 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.
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.


Line 65: Line 69:
</syntaxhighlight>
</syntaxhighlight>


==Line wrapping==
=== Line wrapping ===
===Function Calls===
==== Function calls ====
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.


Line 83: Line 87:
                     5, 8, a, b, c, d)</syntaxhighlight>
                     5, 8, a, b, c, d)</syntaxhighlight>


===Nested function calls===
==== Nested function calls ====
The following function call
The following function call


Line 111: Line 115:
</syntaxhighlight>
</syntaxhighlight>


===Table literals===
==== Table literals ====
Treat these in the same way as function calls.
Treat these in the same way as function calls.


Line 125: Line 129:
</syntaxhighlight>
</syntaxhighlight>


== Semicolons ==
=== Semicolons ===
Besides [[#Table Commas|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|Whitespace Ambiguities]].
Besides [[#Table Commas|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|Whitespace Ambiguities]].


Line 142: Line 146:
print("first"; "second"); -- in function calls
print("first"; "second"); -- in function calls
</syntaxhighlight>
</syntaxhighlight>
==See also==
== See also ==
*[http://lua-users.org/wiki/LuaStyleGuide Lua Style Guide]
* [http://lua-users.org/wiki/LuaStyleGuide Lua users Lua Style guide]
*[[Whitespace]]
* [[Whitespace]]