Skip to content

Commit f6b86c8

Browse files
committed
syncing with src/CodingStyle
1 parent cbedf4d commit f6b86c8

1 file changed

Lines changed: 101 additions & 34 deletions

File tree

docs/src/code/style-guide.adoc

Lines changed: 101 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ Do not use an editor that makes unneeded changes to whitespace (e.g.,
3838
which replaces 8 spaces with a tabstop on a line not otherwise
3939
modified, or word-wraps lines not otherwise modified).
4040

41+
=== Translations
42+
43+
All program code and documentation is written in US English.
44+
All translations (which soon includes the documentation) are performed
45+
with the help of the (https://hosted.weblate.org/projects/linuxcnc/)[weblate]
46+
services. Our users and developers are kindly asked to help with the
47+
translation. The source tree then maintains the .po files in sync with that online
48+
representation.
49+
50+
51+
52+
== C/C++ coding standards
53+
54+
Most of this style guide has been copied wholesale from the Linux kernel
55+
documents - See linux/Documentation/CodingStyle for the full text.
56+
The C++ recommendations have been drawn from various sources such as the
57+
GTK headers, "C++ The Complete Reference" by Schildt, and IRC discussions.
58+
4159
=== Tab Stops
4260

4361
A tab stop always corresponds to 8 spaces. Do not write code that
@@ -48,55 +66,68 @@ displays correctly only with a differing tab stop setting.
4866
Use 4 spaces per level of indentation. Combining 8 spaces into one tab
4967
is acceptable but not required.
5068

51-
=== Translations
69+
Rationale: The whole idea behind indentation is to clearly define where
70+
a block of control starts and ends. Especially when you've been looking
71+
at your screen for 20 straight hours, you'll find it a lot easier to see
72+
how the indentation works if you have large indentations.
5273

53-
All program code and documentation is written in US English.
54-
All translations (which soon includes the documentation) are performed
55-
with the help of the (https://hosted.weblate.org/projects/linuxcnc/)[weblate]
56-
services. Our users and developers are kindly asked to help with the
57-
translation. The source tree then maintains the .po files in sync with that online
58-
representation.
59-
60-
== Code implementing functionality for the machine
74+
Now, some people will claim that having 8-character indentations makes
75+
the code move too far to the right, and makes it hard to read on a
76+
80-character terminal screen. Whilst the GNU style of 2-character
77+
indentations reduces the clarity. For EMC2, a compromise of 4-character
78+
indentation has been chosen. This still spreads the code out and causes
79+
lines to wrap round leading to difficulties in reading the sources. The
80+
answer to that is that if you need more than 3 levels of indentation, you're
81+
screwed anyway, and should fix your program.
6182

6283
=== Placing Braces
6384

64-
Put the opening brace last on the line, and put the closing brace first:
65-
85+
The other issue that always comes up in C styling is the placement of
86+
braces. Unlike the indent size, there are few technical reasons to
87+
choose one placement strategy over the other, but the preferred way, as
88+
shown to us by the prophets Kernighan and Ritchie, is to put the opening
89+
brace last on the line, and put the closing brace first, thusly:
6690
[source,c]
6791
----
68-
if (x) {
69-
// do something appropriate
92+
if (x is true) {
93+
we do y
7094
}
7195
----
72-
73-
The closing brace is on a line of its own, except in the cases where
74-
it is followed by a continuation of the same statement, i.e. a 'while'
75-
in a do-statement or an 'else' in an if-statement, like this:
76-
96+
However, there is one special case, namely functions: they have the
97+
opening brace at the beginning of the next line, thus:
98+
[source,c]
99+
----
100+
int function(int x)
101+
{
102+
body of function
103+
}
104+
----
105+
Note that the closing brace is empty on a line of its own, _except_ in
106+
the cases where it is followed by a continuation of the same statement,
107+
i.e. a "while" in a do-statement or an "else" in an if-statement, like
108+
this:
77109
[source,c]
78110
----
79111
do {
80-
// something important
81-
} while (x > 0);
112+
body of do-loop
113+
} while (condition);
82114
----
83-
84115
and
85-
86116
[source,c]
87117
----
88118
if (x == y) {
89-
// do one thing
90-
} else if (x < y) {
91-
// do another thing
119+
..
120+
} else if (x > y) {
121+
...
92122
} else {
93-
// do a third thing
123+
....
94124
}
95125
----
96-
97-
This brace-placement also minimizes the number of empty (or almost
98-
empty) lines, which allows a greater amount of code or comments to be
99-
visible at once in a terminal of a fixed size.
126+
Also, note that this brace-placement also minimizes the number of empty
127+
(or almost empty) lines, without any loss of readability. Thus, as the
128+
supply of new-lines on your screen is not a renewable resource (think
129+
25-line terminal screens here), you have more empty lines to put
130+
comments on.
100131

101132
=== Naming
102133

@@ -226,7 +257,7 @@ be avoided as much as possible. Rationale: Clarifies which are
226257
variables and which are methods. Public: e.g., axislimit Private: e.g.,
227258
maxvelocity_ .
228259

229-
=== Specific method naming conventions
260+
.Specific method naming conventions
230261

231262
The terms get and set should be used where an attribute is accessed
232263
directly. Rationale: Indicates the purpose of the function or method,
@@ -266,15 +297,51 @@ Implicit tests for zero should not be used except for boolean
266297
variables, e.g., `if (spindle_speed != 0)` NOT `if (spindle_speed)`.
267298

268299
Only loop control statements must be included in a for() construct,
269-
e.g., `sum = 0; for (i=0; i<10; i++) { sum += value[i]; }` +
270-
NOT: `for (i=0, sum=0; i<10; i++) sum += value[i];`.
300+
e.g.
301+
[source,c]
302+
----
303+
sum = 0;
304+
for (i = 0; i < 10; i++) {
305+
sum += value[i];
306+
}
307+
----
308+
NOT:
309+
[source,c]
310+
----
311+
for (i = 0, sum = 0; i < 10; i++) {
312+
sum += value[i];
313+
}
314+
----
271315

272316
Likewise, executable statements in conditionals must be avoided, e.g.,
273-
`if (fd = open(file_name)` is bad.
317+
`if (fd = open(file_name))` is bad.
274318

275319
Complex conditional statements should be avoided - Introduce temporary
276320
boolean variables instead.
277321

322+
The form `while(true)`` should be used for infinite loops.
323+
e.g.
324+
[source,c]
325+
----
326+
while (true) {
327+
...;
328+
}
329+
----
330+
NOT
331+
[source,c]
332+
----
333+
for (;;) {
334+
...;
335+
}
336+
----
337+
or
338+
[source,c]
339+
----
340+
while (1) {
341+
...;
342+
}
343+
----
344+
278345
Parentheses should be used in plenty in mathematical expressions - Do
279346
not rely on operator precedence when an extra parentheses would clarify
280347
things.

0 commit comments

Comments
 (0)