Skip to content

Commit 352fee1

Browse files
committed
docs: Add note to keep header files as lean as possible to CODING_STYLE.md
1 parent c7e42c2 commit 352fee1

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/CODING_STYLE.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,48 @@ SPDX-License-Identifier: LGPL-2.1-or-later
302302
#include "manager.h"
303303
```
304304
305+
- Please keep header files as lean as possible. Prefer implementing functions in
306+
the implementation (.c) file over implementing them in the corresponding
307+
header file. Inline functions in the header are allowed if they are just a few
308+
lines and don't require including any extra header files that would otherwise
309+
not have to be included. Similarly, prefer forward declarations of structs
310+
over including the corresponding header file. Keeping header files as lean as
311+
possible speeds up incremental builds when header files are changed (either by
312+
yourself when working on a pull request or as part of rebasing onto the main
313+
branch) as each file that (transitively) includes a header that was changed
314+
needs to be recompiled. By keeping the number of header files included by
315+
other header files low, we reduce the impact of modifying header files on
316+
incremental builds as much as possible.
317+
318+
Bad:
319+
320+
```c
321+
// source.h
322+
323+
#include "log.h"
324+
325+
static inline void my_function_that_logs(void) {
326+
log_error("oops");
327+
}
328+
```
329+
330+
Good:
331+
332+
```c
333+
// source.h
334+
335+
void my_function_that_logs(void);
336+
337+
// source.c
338+
339+
#include "header.h"
340+
#include "log.h"
341+
342+
void my_function_that_logs(void) {
343+
log_error("oops");
344+
}
345+
```
346+
305347
- The order in which header files are included doesn't matter too
306348
much. systemd-internal headers must not rely on an include order, so it is
307349
safe to include them in any order possible. However, to not clutter global

0 commit comments

Comments
 (0)