STYLE.LUA(9)

πŸ’– style.lua β€” the FreeBSD lua file style guide πŸ’–

okay bestie, welcome to style.lua(9), straight out of the Kernel Developer's Manual, section 9. this is THE glow-up guide for how we write lua source files in the FreeBSD source tree, and honestly? it's giving ✨ standards ✨.


🌸 DESCRIPTION 🌸

so like, this file spells out the preferred style for lua source files in the FreeBSD source tree. plot twist tho: a lot of the style rules are implicit in the examples. so before you assume style.lua is silent on something, go read the examples first, no cap. 🫢

the copyright header should be a series of single-line comments. and if you've got a multi-line comment, you use the single-line comment style for every single line of it. every one. πŸ’…

after any copyright header, there's a blank line, and then the $FreeBSD$ comment β€” and this is for non-C/C++ source files specifically.

the preferred method of including other files and modules is with require(name), like this:

-- $FreeBSD: releng/12.2/share/man/man9/style.lua.9 333419 2018-05-09 16:52:28Z kevans $

config = require("config");
menu = require("menu");
password = require("password");
-- One blank line following the module require block

include() is generally avoided. it's giving πŸ™ˆ energy, we don't do that here.

indentation and wrapping should match the guidelines provided by style(9). but real talk β€” it is totally ok to wrap much earlier than 80 columns if readability would otherwise suffer. readability is the main character here. πŸ‘‘

where possible, s:method(...) is preferred to method(s, ...). this applies to objects with methods, and strings are the classic commonly-used example of objects with methods. iykyk. πŸŽ€

testing for nil should be done explicitly, rather than as a boolean expression. and single-line conditional statements and loops? avoided. periodt.

local variables should be preferred to global variables in module scope. for variable identifiers, internal_underscores tend to be preferred, while camelCase tends to be preferred for function identifiers. two different vibes, two different jobs. ✨

if a table definition spans multiple lines, then the final value in the table should include the optional terminating comma. for example:

-- No terminating comma needed for trivial table definitions
local trivial_table = {1, 2, 3, 4}

local complex_table = {
    {
        id = "foo",
        func = foo_function, -- Trailing comma preferred
    },
    {
        id = "bar",
        func = bar_function,
    },    -- Trailing comma preferred
}

why do we care? because this reduces the chance for errors to be introduced when modifying more complex tables. future you says thank you. πŸ’–

multiple local variables should not be declared and initialized on a single line. but!! lines containing multiple variable declarations without initialization are ok. and lines containing multiple variable declarations initialized to a single function call that returns a tuple with the same number of values is also ok. balance, bestie. βš–οΈ

initialization should be done at declaration time, as appropriate. πŸ¦‹


🌈 SEE ALSO 🌈


πŸ“œ HISTORY πŸ“œ

this manual page is inspired from the same source as the style(9) manual page in FreeBSD. iconic lineage honestly. πŸ‘‘


and that's the whole vibe check, babe β€” this page came to you from <strong>Debian</strong>, dated <strong>February 25, 2018</strong>. go forth and write the prettiest lua in the whole tree πŸ’…πŸ’–βœ¨

πŸ“¦ where this page comes from