xref: /freebsd/share/man/man9/style.lua.9 (revision f0fb94abca6106e18ceb83ce74c27ce9f455db88)
1037f68a9SKyle Evans.\"-
2*f0fb94abSKyle Evans.\" SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*f0fb94abSKyle Evans.\"
4037f68a9SKyle Evans.\" Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
5037f68a9SKyle Evans.\"
6037f68a9SKyle Evans.\" Redistribution and use in source and binary forms, with or without
7037f68a9SKyle Evans.\" modification, are permitted provided that the following conditions
8037f68a9SKyle Evans.\" are met:
9037f68a9SKyle Evans.\" 1. Redistributions of source code must retain the above copyright
10037f68a9SKyle Evans.\"    notice, this list of conditions and the following disclaimer.
11037f68a9SKyle Evans.\" 2. Redistributions in binary form must reproduce the above copyright
12037f68a9SKyle Evans.\"    notice, this list of conditions and the following disclaimer in the
13037f68a9SKyle Evans.\"    documentation and/or other materials provided with the distribution.
14037f68a9SKyle Evans.\"
15037f68a9SKyle Evans.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16037f68a9SKyle Evans.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17037f68a9SKyle Evans.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18037f68a9SKyle Evans.\" ARE DISCLAIMED.  IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
19037f68a9SKyle Evans.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20037f68a9SKyle Evans.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21037f68a9SKyle Evans.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22037f68a9SKyle Evans.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23037f68a9SKyle Evans.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24037f68a9SKyle Evans.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25037f68a9SKyle Evans.\" SUCH DAMAGE.
26037f68a9SKyle Evans.\"
27037f68a9SKyle Evans.\" $FreeBSD$
28037f68a9SKyle Evans.\"
2911d5ba38SKyle Evans.Dd February 25, 2018
30037f68a9SKyle Evans.Dt STYLE.LUA 9
31037f68a9SKyle Evans.Os
32037f68a9SKyle Evans.Sh NAME
33037f68a9SKyle Evans.Nm style.lua
34037f68a9SKyle Evans.Nd
35037f68a9SKyle Evans.Fx
36037f68a9SKyle Evanslua file style guide
37037f68a9SKyle Evans.Sh DESCRIPTION
38037f68a9SKyle EvansThis file specifies the preferred style for lua source files in the
39037f68a9SKyle Evans.Fx
40037f68a9SKyle Evanssource tree.
41037f68a9SKyle EvansMany of the style rules are implicit in the examples.
42037f68a9SKyle EvansBe careful to check the examples before assuming that
43037f68a9SKyle Evans.Nm
44037f68a9SKyle Evansis silent on an issue.
45037f68a9SKyle Evans.Pp
46037f68a9SKyle EvansThe copyright header should be a series of single-line comments.
47037f68a9SKyle EvansUse the single-line comment style for every line in a multi-line comment.
48037f68a9SKyle Evans.Pp
49037f68a9SKyle EvansAfter any copyright header, there is a blank line, and the
50037f68a9SKyle Evans.Li $\&FreeBSD$
51037f68a9SKyle Evanscomment for non-C/C++ source files.
52037f68a9SKyle Evans.Pp
53037f68a9SKyle EvansThe preferred method of including other files and modules is with
54037f68a9SKyle Evans.Fn require name ,
55037f68a9SKyle Evanssuch as:
56037f68a9SKyle Evans.Bd -literal
57037f68a9SKyle Evans-- $FreeBSD$
58037f68a9SKyle Evans
59037f68a9SKyle Evansconfig = require("config");
60037f68a9SKyle Evansmenu = require("menu");
61037f68a9SKyle Evanspassword = require("password");
62037f68a9SKyle Evans-- One blank line following the module require block
63037f68a9SKyle Evans.Ed
64037f68a9SKyle Evans.Pp
65037f68a9SKyle Evans.Fn include
66037f68a9SKyle Evansis generally avoided.
67037f68a9SKyle Evans.Pp
68037f68a9SKyle EvansIndentation and wrapping should match the guidelines provided by
69037f68a9SKyle Evans.Xr style 9 .
70e84e7ed5SKyle EvansDo note that it is ok to wrap much earlier than 80 columns if readability would
71e84e7ed5SKyle Evansotherwise suffer.
72037f68a9SKyle Evans.Pp
73037f68a9SKyle EvansWhere possible,
74037f68a9SKyle Evans.Fn s:method ...
75037f68a9SKyle Evansis preferred to
76037f68a9SKyle Evans.Fn method s ... .
77037f68a9SKyle EvansThis is applicable to objects with methods.
78037f68a9SKyle EvansString are a commonly-used example of objects with methods.
79037f68a9SKyle Evans.Pp
80037f68a9SKyle EvansTesting for
81037f68a9SKyle Evans.Va nil
82037f68a9SKyle Evansshould be done explicitly, rather than as a boolean expression.
83037f68a9SKyle EvansSingle-line conditional statements and loops should be avoided.
84037f68a9SKyle Evans.Pp
85037f68a9SKyle Evans.Ic local
86bb4b11cfSKyle Evansvariables should be preferred to global variables in module scope.
8711d5ba38SKyle Evansinternal_underscores tend to be preferred for variable identifiers, while
8811d5ba38SKyle EvanscamelCase tends to be preferred for function identifiers.
8911d5ba38SKyle Evans.Pp
9011d5ba38SKyle EvansIf a table definition spans multiple lines, then the final value in the table
9111d5ba38SKyle Evansshould include the optional terminating comma.
9211d5ba38SKyle EvansFor example:
9311d5ba38SKyle Evans.Bd -literal
9411d5ba38SKyle Evans-- No terminating comma needed for trivial table definitions
9511d5ba38SKyle Evanslocal trivial_table = {1, 2, 3, 4}
9611d5ba38SKyle Evans
9711d5ba38SKyle Evanslocal complex_table = {
9811d5ba38SKyle Evans	{
9911d5ba38SKyle Evans		id = "foo",
10011d5ba38SKyle Evans		func = foo_function, -- Trailing comma preferred
10111d5ba38SKyle Evans	},
10211d5ba38SKyle Evans	{
10311d5ba38SKyle Evans		id = "bar",
10411d5ba38SKyle Evans		func = bar_function,
10511d5ba38SKyle Evans	},	-- Trailing comma preferred
10611d5ba38SKyle Evans}
10711d5ba38SKyle Evans.Ed
10811d5ba38SKyle Evans.Pp
10911d5ba38SKyle EvansThis reduces the chance for errors to be introduced when modifying more complex
11011d5ba38SKyle Evanstables.
111037f68a9SKyle Evans.Pp
112037f68a9SKyle EvansMultiple local variables should not be declared
113037f68a9SKyle Evans.Sy and
114037f68a9SKyle Evansinitialized on a single line.
115037f68a9SKyle EvansLines containing multiple variable declarations without initialization are ok.
116037f68a9SKyle EvansLines containing multiple variable declarations initialized to a single function
117037f68a9SKyle Evanscall returning a tuple with the same number of values is also ok.
118037f68a9SKyle Evans.Pp
119037f68a9SKyle EvansInitialization
120037f68a9SKyle Evans.Sy should
121037f68a9SKyle Evansbe done at declaration time as appropriate.
122037f68a9SKyle Evans.Sh SEE ALSO
123037f68a9SKyle Evans.Xr style 9
124037f68a9SKyle Evans.Sh HISTORY
125037f68a9SKyle EvansThis manual page is inspired from the same source as
126037f68a9SKyle Evans.Xr style 9
127037f68a9SKyle Evansmanual page in
128037f68a9SKyle Evans.Fx .
129