aboutsummaryrefslogtreecommitdiffhomepage
path: root/markup/pod/live-manual/media/text/en/project_coding-style.ssi
blob: 9a03971e991f81a01e72ff5a5a828c912c145a17 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
:B~ Coding Style

1~coding-style Coding Style

This chapter documents the coding style used in live systems.

2~ Compatibility

_* Don't use syntax or semantics that are unique to the Bash shell. For example, the use of array constructs.

_* Only use the POSIX subset - for example, use $(foo) over `foo`.

_* You can check your scripts with 'sh -n' and 'checkbashisms'.

_* Make sure all shell code runs with 'set -e'.

2~ Indenting

_* Always use tabs over spaces.

2~ Wrapping

_* Generally, lines are 80 chars at maximum.

_* Use the "Linux style" of line breaks:

Bad:

code{

 if foo; then
         bar
 fi

}code

Good:

code{

 if foo
 then
         bar
 fi

}code

_* The same holds for functions:

Bad:

code{

 Foo () {
         bar
 }

}code

Good:

code{

 Foo ()
 {
         bar
 }

}code

2~ Variables

_* Variables are always in capital letters.

_* Variables used in live-build always start with #{LB_}# prefix.

_* Internal temporary variables in live-build should start with the #{\_LB_}# prefix.

_* Local variables start with live-build #{\_\_LB_}# prefix.

_* Variables in connection to a boot parameter in live-config start with #{LIVE_}#.

_* All other variables in live-config start with #{_}# prefix.

_* Use braces around variables; e.g. write #{${FOO}}# instead of #{$FOO}#.

_* Always protect variables with quotes to respect potential whitespaces: write #{"${FOO}"}# not #{${FOO}}#.

_* For consistency reasons, always use quotes when assigning values to variables:

Bad:

code{

 FOO=bar

}code

Good:

code{

 FOO="bar"

}code

_* If multiple variables are used, quote the full expression:

Bad:

code{

 if [ -f "${FOO}"/foo/"${BAR}"/bar ]
 then
         foobar
 fi

}code

Good:

code{

 if [ -f "${FOO}/foo/${BAR}/bar" ]
 then
         foobar
 fi

}code

2~ Miscellaneous

_* Use "#{|}#" (without the surround quotes) as a separator in calls to sed, e.g. "#{sed -e 's|foo|bar|'}#" (without "").

_* Don't use the #{test}# command for comparisons or tests, use "#{[}#" "#{]}#" (without ""); e.g. "#{if [ -x /bin/foo ]; ...}#" and not "#{if test -x /bin/foo; ...}#".

_* Use #{case}# wherever possible over #{test}#, as it's easier to read and faster in execution.

_* Use capitalized names for functions to limit messing with the users environment.