xref: /freebsd/share/man/man9/style.lua.9 (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
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, and the
48.Li $\&FreeBSD$
49comment for non-C/C++ source files.
50.Pp
51The preferred method of including other files and modules is with
52.Fn require name ,
53such as:
54.Bd -literal
55
56config = require("config");
57menu = require("menu");
58password = require("password");
59-- One blank line following the module require block
60.Ed
61.Pp
62.Fn include
63is generally avoided.
64.Pp
65Indentation and wrapping should match the guidelines provided by
66.Xr style 9 .
67Do note that it is ok to wrap much earlier than 80 columns if readability would
68otherwise suffer.
69.Pp
70Where possible,
71.Fn s:method ...
72is preferred to
73.Fn method s ... .
74This is applicable to objects with methods.
75String are a commonly-used example of objects with methods.
76.Pp
77Testing for
78.Va nil
79should be done explicitly, rather than as a boolean expression.
80Single-line conditional statements and loops should be avoided.
81.Pp
82.Ic local
83variables should be preferred to global variables in module scope.
84internal_underscores tend to be preferred for variable identifiers, while
85camelCase tends to be preferred for function identifiers.
86.Pp
87If a table definition spans multiple lines, then the final value in the table
88should include the optional terminating comma.
89For example:
90.Bd -literal
91-- No terminating comma needed for trivial table definitions
92local trivial_table = {1, 2, 3, 4}
93
94local complex_table = {
95	{
96		id = "foo",
97		func = foo_function, -- Trailing comma preferred
98	},
99	{
100		id = "bar",
101		func = bar_function,
102	},	-- Trailing comma preferred
103}
104.Ed
105.Pp
106This reduces the chance for errors to be introduced when modifying more complex
107tables.
108.Pp
109Multiple local variables should not be declared
110.Sy and
111initialized on a single line.
112Lines containing multiple variable declarations without initialization are ok.
113Lines containing multiple variable declarations initialized to a single function
114call returning a tuple with the same number of values is also ok.
115.Pp
116Initialization
117.Sy should
118be done at declaration time as appropriate.
119.Sh SEE ALSO
120.Xr style 9
121.Sh HISTORY
122This manual page is inspired from the same source as
123.Xr style 9
124manual page in
125.Fx .
126