User:Ganaram inukshuk/Provisional style guide for Lua: Difference between revisions
No edit summary |
|||
| Line 1: | Line 1: | ||
Parts of it are adapted | The following style guide is a provisional guide adapted from the LuaRocks style guide: https://github.com/luarocks/lua-style-guide | ||
Parts of it are adapted for use on the wiki, with details on specific conventions. If a specific convention is missing, fall back to that described in the Luarocks style guide. | |||
== Preliminaries == | |||
=== Indentation and formatting === | === Indentation and formatting === | ||
The in-browser Lua editor uses tabs for indenting, which equates to 4 spaces. | |||
=== | === Declaring and naming variables === | ||
Variable names should be short and descriptive, written <code>snake_case</code>. Exceptions to this include counter variables, like with <code>i</code> in for loops. Boolean variables are prefixed with <code>is_</code>. Constants are named using <code>TRAIN_CASE</code>. | |||
All variable declarations should start with <code>local</code>. | |||
=== Strings === | === Strings === | ||
Strings are enclosed in double quotes <code>" "</code>. If a string contains double quotes, do not escape them while enclosing the string in double quotes, as this reduces readability; instead, enclose the string in single quotes <code>' '</code>. Escape characters are only necessary if the string contains both single and double quotes. | |||
'''Preferred''' | |||
<syntaxhighlight lang="lua">local sentence = "That's his cheese quesadilla!" | |||
local quotation = "And he said to me, \"One man's trash is another man's treasure.\"" | |||
</syntaxhighlight> | |||
'''Avoid''' | |||
<syntaxhighlight lang="lua">local formatting = "padding: 0.25em 0.5em; border: 1px solid white;\" colspan="2\" | " | |||
</syntaxhighlight> | |||
=== Line lengths === | === Line lengths === | ||
=== | The 80-column limit is to be obeyed wherever possible for both comments and code, except for cases where strings, inline comments, or function calls are too long. | ||
<syntaxhighlight lang="lua"> | |||
local function navbox_title() | |||
if not title then return "" end | |||
local has_navbar = name ~= nil | |||
return "|-\n" .. | |||
'! style="text-align: center; background-color: #eaecf0; white-space: nowrap; margin: 0em 4em 0em 4em;' .. | |||
'padding: 0.25em 0.5em; border: 1px solid white;" colspan="2" | ' .. | |||
'<span style="display: inline-block; float: left; text-align: left; font-weight: normal; font-style: normal; min-width: 4em; padding: 0px; margin: 0px;">' .. | |||
(has_navbar and navbar(name, "mini", "") or "") .. '</span>' .. | |||
'<span style="font-size: 1.05em;">' .. title .. '</span>' .. | |||
(is_collapsible and '' or '<span style="display: inline-block; float: right; font-size: 0.8em; width: 5em;"> </span>') | |||
end | |||
</syntaxhighlight> | |||
=== Naming, declaring, and calling functions === | |||
As with variables, functions are named using <code>snake_case</code>. | |||
''tbd'' | |||
=== Table attributes === | === Table attributes === | ||