Debian Codegraph @

doc/antora/modules/developers/pages/coding-methods.adoc

raw
1 = Coding Methods
2
3 The following is a short set of guidelines to follow while programming. It does
4 not address coding styles, function naming methods, or debugging methods.
5 Rather, it describes the processes which should go on in the programmer’s mind
6 while they are programming.
7
8 Coding standards apply to function names, the look of the code, and coding
9 consistency. Coding methods apply to the daily practices used by the programmer
10 to write code.
11
12 == Comment your code
13
14 If you don’t, you’ll be forced to debug it six months later, when you have no clue
15 as to what it’s doing.
16
17 If someone _really_ hates you, you’ll be forced to debug un-commented code that
18 someone else wrote. You don’t want to do that.
19
20 For FreeRADIUS, use doxygen `@`-style comments so you get the
21 benefits of the developer documentation https://doc.freeradius.org/[site].
22
23 == Give things reasonable names
24
25 Variables and functions should have names. Calling them `x`, `xx`,
26 and `xxx` makes your life hell. Even `foo` and `i` are
27 problematic.
28
29 Avoid smurfs. Don’t re-use struct names in field names, i. e.
30
31 [source,c]
32 ----
33 struct smurf {
34 char *smurf_pappa_smurf;
35 }
36 ----
37
38 If your code reads as full English sentences, you’re doing it right.
39
40 == Check input parameters in the functions you write
41
42 Your function _cannot_ do anything right if the user passed in garbage
43 and you were too lazy to check for garbage input.
44
45 `assert()` (`fr_assert()`) is ugly. Use it.
46
47 [NOTE]
48 ====
49 GIGO, "Garbage In, Garbage Out," is wrong. If your function gets
50 garbage input, it should complain loudly and with great
51 descriptiveness.
52 ====
53
54 == Write useful error messages
55
56 "Function failed" is useless as an error message. It makes debugging the code
57 impossible without source-level instrumentation.
58
59 If you’re going to instrument the code at source level for error messages, leave
60 the error messages there, so the next sucker won’t have to do the same work all
61 over again.
62
63 == Check error conditions from the functions you call
64
65 Your function _cannot_ do anything right if you called another function, and
66 they gave you garbage output.
67
68 One of the most common mistakes is:
69
70 [source,c]
71 ----
72 fp = fopen(...);
73 fgetc(fp); /* core dumps! */
74 ----
75
76 If the programmer had bothered to check for a `NULL` fp (error
77 condition), then they could have produced a descriptive error message
78 instead of having the program core dump.
79
80 == Core dumps are *ineffectual*
81
82 If your program core dumps accidentally, it's an indication of bad or weak programming. You don’t know what your program is doing, or what it’s supposed to be doing when anything goes wrong.
83
84 If it hits an `assert()` and calls `abort()`, you’re a genius. You’ve thought
85 ahead to what _might_ go wrong, and put in an assertion to ensure that it fails
86 in a _known manner_ when something _does_ go wrong. (As it usually does…)
87
88 == Initialize your variables
89
90 `memset()` (`talloc_zero()`) is your friend. `ptr = NULL` is nice,
91 too.
92
93 Having variables containing garbage values makes it easy for the code to do
94 garbage things. The contents of local variables are inputs to your function. See
95 xref:#_check_input_parameters_in_the_functions_you_write[#3].
96
97 It’s also nearly impossible for you to debug any problems, as you can’t tell the
98 variables with garbage values from the real ones.
99
100 == Don’t allow buffer over-runs
101
102 They’re usually accidental, but they cause core dumps. `strcpy()` and `strcat()`
103 are ugly. Use them under duress.
104
105 `sizeof()` is your friend.
106
107 == `const` is your friend
108
109 If you don’t mean to modify an input structure to your function, declare it
110 `const`. Declare string constants `const`. It can’t hurt, and it allows more
111 errors to be found at compile time.
112
113 Use `const` everywhere. Once you throw a few into your code, and have it save
114 you from stupid bugs, you’ll blindly throw in `const` everywhere. It’s a
115 life-saver.
116
117 == Use C compiler warnings
118
119 Turn on all of the C compiler warnings possible. You might have to turn some off
120 due to broken system header files, though. But the more warnings the merrier.
121
122 Getting error messages at compile time is much preferable to getting core dumps
123 at run time. See xref:#_initialize_your_variables[#7].
124
125 Notice that the C compiler error messages are helpful? You should write error
126 messages like this, too. See xref:#_write_useful_error_messages[#4].
127
128 == Avoid UNIXisms and ASCIIisms and visualisms
129
130 You don’t know under what system someone will try to run your code. Don’t demand
131 that others use the same OS or character set as you use.
132
133 Never assign numbers to pointers. If foo is a `char*`, and you want it to be be
134 `NULL`, assign `NULL`, not `0`. The zeroth location is perfectly as addressable
135 as any other on plenty of OSes. Not all the world runs on Unix (though it should
136 :) ).
137
138 Another common mistake is to assume that the zeroth character in the character
139 set is the string terminator. Instead of terminating a string with `0`, use
140 `'\0'`, which is always right. Similarly, `memset()` with the appropriate value:
141 `NULL`, `'\0'`, or `0` for pointers, chars, and numbers.
142
143 Don’t put tabs in string constants, either. Always use `'\t'` to represent a
144 tab, instead of ASCII 9. Literal tabs are presented to readers of your code as
145 arbitrary whitespace, and it’s easy to mess up.
146
147 == Make conditionals explicit
148
149 Though it’s legal to test `if (foo) {}` if you test against the appropriate
150 value (like `NULL` or `'\0'`), your code is prettier and easier for others to
151 read without having to eyeball your prototypes continuously to figure out what
152 you’re doing (especially if your variables aren’t well-named). See
153 xref:#_give_things_reasonable_names[#2].
154
155 == Test your code
156
157 Even Donald Knuth writes buggy code. You’ll never find all of the bugs in your
158 code. But writing a test program definitely helps.
159
160 This means that you’ll have to write your code so that it will be easily
161 testable. As a result, it will look better and be easier to debug.
162
163 == Hints, Tips, and Tricks
164
165 This section lists many of the common `rules` associated with code submitted to
166 the project. There are always exceptions… but you must have a really good reason
167 for doing so.
168
169 == Read the Documentation and follow the CodingStyle
170
171 The FreeRADIUS server has a common coding style. Use real tabs to indent. There
172 is whitespace in variable assignments (`i = 1`, not `i=1`).
173
174 When in doubt, format your code to look the same as code already in the server.
175 If your code deviates too much from the current style, it is likely to be
176 rejected without further review, and without comment.
177
178 == #ifdefs are ugly
179
180 Code cluttered with `#ifdef` s is difficult to read and maintain. Don’t do it.
181 Instead, put your `#ifdef` s in a header, and conditionally define `static
182 inline` functions, or macros, which are used in the code. Let the compiler
183 optimize away the 'no-op' case.
184
185 Simple example, of poor code:
186
187 [source,c]
188 ----
189 #ifdef CONFIG_MY_FUNKINESS
190 init_my_stuff(foo);
191 #endif
192 ----
193
194 Cleaned-up example:
195
196 (in header):
197
198 [source,c]
199 ----
200 #ifndef CONFIG_MY_FUNKINESS
201 static inline void init_my_stuff(char *foo) {}
202 #endif
203 ----
204
205 (in the code itself):
206
207 [source,c]
208 ----
209 init_my_stuff(dev);
210 ----
211
212 == `static inline` is better than a macro
213
214 Static inline functions are greatly preferred over macros. They provide type
215 safety, have no length limitations, no formatting limitations, and under gcc
216 they are as cheap as macros.
217
218 Macros should only be used for cases where a static inline is clearly suboptimal
219 (there a few, isolated cases of this in fast paths), or where it is impossible
220 to use a static inline function (such as string-izing).
221
222 `static inline` is preferred over `$$static __inline__$$`, `extern inline`, and
223 `$$extern __inline__$$`.
224
225 == Don’t over-design
226
227 Don’t try to anticipate nebulous future cases which may or may not be useful:
228 _Make it as simple as you can, and no simpler._
229
230 Split up functionality as much as possible. If your code needs to do two
231 unrelated things, write two functions. Mashing two kinds of work into one
232 function makes the server difficult to debug and maintain.
233