-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoding_practice.txt
110 lines (93 loc) · 2.3 KB
/
coding_practice.txt
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
#define EXPRESSION (0)
#define BLOCK EXPRESSION; EXPRESSION; EXPRESSION;
/*
Struct definitions must be of the form `<structname>_s`.
Type definitions must be of the form `<typename>_t`.
Enum definitions must be of the form `<enumname>_e`.
Labels must be of the form `<label>_l`.
Globals must be of the form `g_<varname>`. This form differs from the rest
because it works better with code completion.
*/
int g_global;
typedef struct struct_s {
int element0;
int element1;
} struct_t;
typedef enum enum_e {
enum_element_A,
enum_element_B,
enum_element_C
} enum_t;
/*
Functions with no arguments must be declared with `void` as the only argument.
*/
int main(void) {
/*
If a function returns an error, `int error` must be declared on the first
line.
*/
int error = 1;
int value;
/*
If statements shall take this form.
*/
if (EXPRESSION) {
BLOCK
}
else if (EXPRESSION) {
BLOCK
}
else {
BLOCK
}
/*
If statements formatted like this are disallowed.
*/
if (EXPRESSION)
EXPRESSION;
else if (EXPRESSION)
EXPRESSION;
else
EXPRESSION;
/*
When practical, the string type should be used exclusively.
*/
/*
If a function is defined in a header file, then it is usually best to
define it as `<headerfile>_<functionName>`
*/
/*
Functions that create something should be named `something_create*`.
Functions that setup something that exists should be named
`something_init*`.
Functions that destroy something should be named `something_delete*`.
Functions that decommission something should be named `something_quit*`.
Functions that free something should be named `something_free*`.
*/
/*
The name of arrays will be plural.
*/
value = EXPRESSION;
if (value != 0) {
/*
To handle a function-ending error, set `error` and jump to `cleanup_l`.
*/
error = 1;
goto cleanup_l;
}
/*
These two lines must be in every function that returns an error.
*/
error = 0;
cleanup_l:
/*
Return values are for errors only. If a function does not return an error,
then it should be declared void, unless that really doesn't make sense.
*/
return error;
}
/*
When using callbacks, make sure every callback has some form of logging. That
way, when Valgrind gives you "Jump to the invalid address stated on the next
line", you should have no trouble finding the offending function.
*/