xref: /freebsd/share/man/man9/style.lua.9 (revision f1bd7311fbd576a387042c0d81945b701c7459a3)
1037f68a9SKyle Evans.\"-
24d846d26SWarner Losh.\" SPDX-License-Identifier: BSD-2-Clause
3f0fb94abSKyle 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.\"
2711d5ba38SKyle Evans.Dd February 25, 2018
28037f68a9SKyle Evans.Dt STYLE.LUA 9
29037f68a9SKyle Evans.Os
30037f68a9SKyle Evans.Sh NAME
31037f68a9SKyle Evans.Nm style.lua
32037f68a9SKyle Evans.Nd
33037f68a9SKyle Evans.Fx
34037f68a9SKyle Evanslua file style guide
35037f68a9SKyle Evans.Sh DESCRIPTION
36037f68a9SKyle EvansThis file specifies the preferred style for lua source files in the
37037f68a9SKyle Evans.Fx
38037f68a9SKyle Evanssource tree.
39037f68a9SKyle EvansMany of the style rules are implicit in the examples.
40037f68a9SKyle EvansBe careful to check the examples before assuming that
41037f68a9SKyle Evans.Nm
42037f68a9SKyle Evansis silent on an issue.
43037f68a9SKyle Evans.Pp
44037f68a9SKyle EvansThe copyright header should be a series of single-line comments.
45037f68a9SKyle EvansUse the single-line comment style for every line in a multi-line comment.
46037f68a9SKyle Evans.Pp
47*f1bd7311SEd MasteAfter any copyright header there is a blank line.
48037f68a9SKyle Evans.Pp
49037f68a9SKyle EvansThe preferred method of including other files and modules is with
50037f68a9SKyle Evans.Fn require name ,
51037f68a9SKyle Evanssuch as:
52037f68a9SKyle Evans.Bd -literal
53*f1bd7311SEd Maste-- License block
54037f68a9SKyle Evans
55037f68a9SKyle Evansconfig = require("config");
56037f68a9SKyle Evansmenu = require("menu");
57037f68a9SKyle Evanspassword = require("password");
58037f68a9SKyle Evans-- One blank line following the module require block
59037f68a9SKyle Evans.Ed
60037f68a9SKyle Evans.Pp
61037f68a9SKyle Evans.Fn include
62037f68a9SKyle Evansis generally avoided.
63037f68a9SKyle Evans.Pp
64037f68a9SKyle EvansIndentation and wrapping should match the guidelines provided by
65037f68a9SKyle Evans.Xr style 9 .
66e84e7ed5SKyle EvansDo note that it is ok to wrap much earlier than 80 columns if readability would
67e84e7ed5SKyle Evansotherwise suffer.
68037f68a9SKyle Evans.Pp
69037f68a9SKyle EvansWhere possible,
70037f68a9SKyle Evans.Fn s:method ...
71037f68a9SKyle Evansis preferred to
72037f68a9SKyle Evans.Fn method s ... .
73037f68a9SKyle EvansThis is applicable to objects with methods.
74037f68a9SKyle EvansString are a commonly-used example of objects with methods.
75037f68a9SKyle Evans.Pp
76037f68a9SKyle EvansTesting for
77037f68a9SKyle Evans.Va nil
78037f68a9SKyle Evansshould be done explicitly, rather than as a boolean expression.
79037f68a9SKyle EvansSingle-line conditional statements and loops should be avoided.
80037f68a9SKyle Evans.Pp
81037f68a9SKyle Evans.Ic local
82bb4b11cfSKyle Evansvariables should be preferred to global variables in module scope.
8311d5ba38SKyle Evansinternal_underscores tend to be preferred for variable identifiers, while
8411d5ba38SKyle EvanscamelCase tends to be preferred for function identifiers.
8511d5ba38SKyle Evans.Pp
8611d5ba38SKyle EvansIf a table definition spans multiple lines, then the final value in the table
8711d5ba38SKyle Evansshould include the optional terminating comma.
8811d5ba38SKyle EvansFor example:
8911d5ba38SKyle Evans.Bd -literal
9011d5ba38SKyle Evans-- No terminating comma needed for trivial table definitions
9111d5ba38SKyle Evanslocal trivial_table = {1, 2, 3, 4}
9211d5ba38SKyle Evans
9311d5ba38SKyle Evanslocal complex_table = {
9411d5ba38SKyle Evans	{
9511d5ba38SKyle Evans		id = "foo",
9611d5ba38SKyle Evans		func = foo_function, -- Trailing comma preferred
9711d5ba38SKyle Evans	},
9811d5ba38SKyle Evans	{
9911d5ba38SKyle Evans		id = "bar",
10011d5ba38SKyle Evans		func = bar_function,
10111d5ba38SKyle Evans	},	-- Trailing comma preferred
10211d5ba38SKyle Evans}
10311d5ba38SKyle Evans.Ed
10411d5ba38SKyle Evans.Pp
10511d5ba38SKyle EvansThis reduces the chance for errors to be introduced when modifying more complex
10611d5ba38SKyle Evanstables.
107037f68a9SKyle Evans.Pp
108037f68a9SKyle EvansMultiple local variables should not be declared
109037f68a9SKyle Evans.Sy and
110037f68a9SKyle Evansinitialized on a single line.
111037f68a9SKyle EvansLines containing multiple variable declarations without initialization are ok.
112037f68a9SKyle EvansLines containing multiple variable declarations initialized to a single function
113037f68a9SKyle Evanscall returning a tuple with the same number of values is also ok.
114037f68a9SKyle Evans.Pp
115037f68a9SKyle EvansInitialization
116037f68a9SKyle Evans.Sy should
117037f68a9SKyle Evansbe done at declaration time as appropriate.
118037f68a9SKyle Evans.Sh SEE ALSO
119037f68a9SKyle Evans.Xr style 9
120037f68a9SKyle Evans.Sh HISTORY
121037f68a9SKyle EvansThis manual page is inspired from the same source as
122037f68a9SKyle Evans.Xr style 9
123037f68a9SKyle Evansmanual page in
124037f68a9SKyle Evans.Fx .
125