xref: /freebsd/share/man/man9/style.lua.9 (revision 4543ef516683042d46f3bd3bb8a4f3f746e00499)
1.\"-
2.\" SPDX-License-Identifier: BSD-2-Clause
3.\"
4.\" Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18.\" ARE DISCLAIMED.  IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE.
26.\"
27.Dd February 25, 2018
28.Dt STYLE.LUA 9
29.Os
30.Sh NAME
31.Nm style.lua
32.Nd
33.Fx
34lua file style guide
35.Sh DESCRIPTION
36This file specifies the preferred style for lua source files in the
37.Fx
38source tree.
39Many of the style rules are implicit in the examples.
40Be careful to check the examples before assuming that
41.Nm
42is silent on an issue.
43.Pp
44The copyright header should be a series of single-line comments.
45Use the single-line comment style for every line in a multi-line comment.
46.Pp
47After any copyright header there is a blank line.
48.Pp
49The preferred method of including other files and modules is with
50.Fn require name ,
51such as:
52.Bd -literal
53-- License block
54
55config = require("config");
56menu = require("menu");
57password = require("password");
58-- One blank line following the module require block
59.Ed
60.Pp
61.Fn include
62is generally avoided.
63.Pp
64Indentation and wrapping should match the guidelines provided by
65.Xr style 9 .
66Do note that it is ok to wrap much earlier than 80 columns if readability would
67otherwise suffer.
68.Pp
69Where possible,
70.Fn s:method ...
71is preferred to
72.Fn method s ... .
73This is applicable to objects with methods.
74String are a commonly-used example of objects with methods.
75.Pp
76Testing for
77.Va nil
78should be done explicitly, rather than as a boolean expression.
79Single-line conditional statements and loops should be avoided.
80.Pp
81.Ic local
82variables should be preferred to global variables in module scope.
83internal_underscores tend to be preferred for variable identifiers, while
84camelCase tends to be preferred for function identifiers.
85.Pp
86If a table definition spans multiple lines, then the final value in the table
87should include the optional terminating comma.
88For example:
89.Bd -literal
90-- No terminating comma needed for trivial table definitions
91local trivial_table = {1, 2, 3, 4}
92
93local complex_table = {
94	{
95		id = "foo",
96		func = foo_function, -- Trailing comma preferred
97	},
98	{
99		id = "bar",
100		func = bar_function,
101	},	-- Trailing comma preferred
102}
103.Ed
104.Pp
105This reduces the chance for errors to be introduced when modifying more complex
106tables.
107.Pp
108Multiple local variables should not be declared
109.Sy and
110initialized on a single line.
111Lines containing multiple variable declarations without initialization are ok.
112Lines containing multiple variable declarations initialized to a single function
113call returning a tuple with the same number of values is also ok.
114.Pp
115Initialization
116.Sy should
117be done at declaration time as appropriate.
118.Sh SEE ALSO
119.Xr style 9
120.Sh HISTORY
121This manual page is inspired from the same source as
122.Xr style 9
123manual page in
124.Fx .
125