summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-06-11 18:23:57 +0200
committerLuca Boccassi <luca.boccassi@gmail.com>2021-06-11 18:45:31 +0100
commit756755d0fc5a0ef380416a54346b6438c3fb7ba5 (patch)
treeb1eae142c5ef7923407c871edf7e0c2009581473 /docs
parente77365b479d6f2df3b10411b87e17ea6ca2ac288 (diff)
downloadsystemd-756755d0fc5a0ef380416a54346b6438c3fb7ba5.tar.gz
docs: update coding style a bit
Say that r should be declared at the top of the function. Don't say that fixed buffers result in truncation, right after saying that they must only be used if size is known. Adjust order of examples to be consistent.
Diffstat (limited to 'docs')
-rw-r--r--docs/CODING_STYLE.md33
1 files changed, 20 insertions, 13 deletions
diff --git a/docs/CODING_STYLE.md b/docs/CODING_STYLE.md
index 847ec40d81..09b74d35d8 100644
--- a/docs/CODING_STYLE.md
+++ b/docs/CODING_STYLE.md
@@ -143,38 +143,46 @@ layout: default
## Using C Constructs
-- Preferably allocate local variables on the top of the block:
+- Allocate local variables where it makes sense: at the top of the block, or at
+ the point where they can be initialized. `r` is typically used for a local
+ state variable, but should almost always be declared at the top of the
+ function.
```c
{
- int a, b;
+ uint64_t a, b;
+ int r;
- a = 5;
- b = a;
+ a = frobnicate();
+ b = a + 5;
+
+ r = do_something();
+ if (r < 0)
+ …
}
```
-- Do not mix function invocations with variable definitions in one line. Wrong:
+- Do not mix function invocations with variable definitions in one line.
```c
{
- int a = foobar();
uint64_t x = 7;
+ int a;
+
+ a = foobar();
}
```
- Right:
+ instead of:
```c
{
- int a;
+ int a = foobar();
uint64_t x = 7;
-
- a = foobar();
}
```
-- Use `goto` for cleaning up, and only use it for that. i.e. you may only jump
+- Use `goto` for cleaning up, and only use it for that. I.e. you may only jump
to the end of a function, and little else. Never jump backwards!
- To minimize strict aliasing violations, we prefer unions over casting.
@@ -347,8 +355,7 @@ layout: default
`log_oom()` for then printing a short message, but not in "library" code.
- Avoid fixed-size string buffers, unless you really know the maximum size and
- that maximum size is small. They are a source of errors, since they possibly
- result in truncated strings. It is often nicer to use dynamic memory,
+ that maximum size is small. It is often nicer to use dynamic memory,
`alloca()` or VLAs. If you do allocate fixed-size strings on the stack, then
it is probably only OK if you either use a maximum size such as `LINE_MAX`,
or count in detail the maximum size a string can have. (`DECIMAL_STR_MAX` and