forked from DOMjudge/domjudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CODINGSTYLE
144 lines (105 loc) · 3.97 KB
/
CODINGSTYLE
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
140
141
142
143
144
DOMjudge project coding style guideline
=======================================
Introduction
============
This document describes the coding style within DOMjudge. The
specified style is not always applicable to all different languages
used within DOMjudge, as it is formulated mostly for C-like languages,
of which C/C++ and PHP are used within this project.
The coding style outlined here is a guideline: we do not strictly
enforce it, but it has a (strong) preference. Where sensible,
deviations are ok, but any committer is requested to globally follow
these guidelines to make the code more consistent, hence more readable.
General style
=============
Generally, we adapt the K&R coding style, which means that braces '{}'
opening and closing function blocks go on separate lines without extra
indentation. Braces for control construct start on the same line as
the control construct and end on a separate line.
The preferred maximum line length is 80 chars to allow others to use
multiple 80 char-width terminals alongside eachother.
Indentation
===========
We use tabs for indentation. Tab size should equal indent size and has
a preferred value of 4 spaces. It is preferred to use tabs for
_indentation_ but spaces for _alignment_, see advocacy below; this
makes correct code display independent of the choice of tab/indent
size. Empty lines should preferably not contain any whitespace.
Spacing
=======
Internal spacing within code:
* A space between control constructs (for, if, while,...) and the
opening parenthesis '('.
* Spaces between test condition outer parentheses and test.
* No space between negation operator '!' and operand.
* A space after comma separating arguments in function calls and
function definitions.
* The ternary operator '(x ? a : b)' must be put within parentheses.
Use this operator carefully as it can reduce readability.
* Spaces around the PHP string concatenation operator '.'.
for (int i=0; i<10; i++) {
if ( !test ) do_something(a, b, c);
x = (test ? 1 : 0);
}
Miscellaneous
=============
We use K&R style for braces placement. This means function opening
braces on a separate new line as an exception, but all other braces
are placed on the same line as their accompanying control constructs.
Short statements can be placed on the same line following a control
construct without braces. Longer or multi-statements should go on
separate lines within braces.
Multiple declarations and (short) statement are allowed on one line.
Vertical alignment of code is encouraged when this increases
readability.
int main(int argc, char **argv)
{
int a, b, a1, b1;
a = 0; a1 = 1;
b = 10; b1 = 11;
if ( test ) {
do_a();
} else {
do_b();
}
for (int i=0; i<10; i++) do_something(i);
return 0;
}
GNU indent options
==================
Here is a (non-exhaustive) list of the GNU indent options we prefer:
--use-tabs
--blank-lines-after-procedures
--no-blank-lines-after-commas
--braces-after-func-def-line
--braces-on-struct-decl-line
--braces-on-if-line
--cuddle-do-while
--cuddle-else
--dont-break-function-decl-args
--dont-break-procedure-type
--space-after-for
--space-after-if
--space-after-while
--no-space-after-function-call-names
--continue-at-parentheses
--break-after-boolean-operator
Indentation/alignment with tabs/spaces
======================================
The use of spaces or tabs to indent code is a hotly debated argument.
Here, I, Jaap Eldering, would like to advocate the use of a style to
use tabs for indentation and spaces for alignment. Some references
explaining this style and its merits:
http://stianse.wordpress.com/2008/11/17/indent-with-tabs-align-with-spaces/
http://boredzo.org/blog/archives/2007-05-07/tabs-vs-spaces
http://www.vim.org/scripts/script.php?script_id=231
The first and third reference also contain code to implement this
style in Emacs and Vim respectively. The code layout will be as
follows ('--->' denotes a tab and '.' spaces):
int main()
{
--->if ( some_long_conditional &&
--->.....and_a_second ) {
--->--->statement;
--->}
}