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