summaryrefslogtreecommitdiff
path: root/doc/coding-standards.txt
blob: 3e4253ce3303670a34187a4248c35a0d5b900b2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Coding standards
================
This document describe the coding standards used in envbot.

Indention
---------
Indention is done with tabs, one tab char for each indention level.
Do adjust the code with spaces if you need to break a long line.
Example:
	thisFunctionGotLotsOfParameters foo bar quux loooooooooooooooooooooooooooongParameter
	                                some more parameters
That is, first indent with tabs to base indention level, then use spaces
to adjust.


Spaces
------
Trailing spaces
	Forbidden.
Spaces around operators:
	Depend on situation.
	Examples:
		myvar="this is a value"
		if [[ "$myvar" = "something" ]]; then
	As you can see it depends on where it is used.
	Generally it should be used everywhere except assignment like
	"=" and "=+".


Newlines
--------
There should not be newlines before a { or other block separator.
For example this is correct:
	if [[ $a = $b ]]; then
		runStuff
	fi
But this is incorrect:
	if [[ $a = $b ]]
	then
		runStuff
	fi

Trailing newlines: There should be a single
ending newline at the end of the file, no
trailing newlines.


Functions
---------
Functions should be declared without the function keyword
and with the () following the name of the function, without
space between the function name and the (). After the () there
should be one space and then the opening bracket.
Example:
myNiceFunction() {
	runStuff
}


Functions: Returning values
---------------------------
Return values using "printf -v" construct. Avoid subshells (for
performance reasons). One exception to this is the case of the
function only calling an external program, then just subshell
the function let the external programs output go to STDOUT of the
function.
Example of printf -v construct (first parameter for this function
is the name of the variable to return in):
	myfunction() {
		printf -v "$1" '%s' "something we computed"
	}
Using return code should only be used when returned value is
an error code. Example:
	otherfunction() {
		if [[ "$1" = "foo" ]]; then
			# Success!
			return 0
		else
			# Fail
			return 1
		fi
	}


Comments
--------
Comments are free form inside function, function comment should
be coded in bashdoc style.
(TODO: add description of that or link to that)

Comments should be indented to same level as surrounding code,
unless the comment is commented out code. Commented out code
should be avoided in committed code, but if it is needed, the
comment should be at the start of the line.


if
--
When testing in if use the [[ ]] construct. Do NOT under ANY
circumstances use the [ ] construct.