summaryrefslogtreecommitdiff
path: root/docs/CODING_STYLE.md
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-07-06 13:44:51 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-11-03 10:30:36 +0100
commit9214f2999b85533ad184e6508f0a0a078d9d3b8f (patch)
treeb1252df6648d9cdb84ced2d21797371e98f34b0b /docs/CODING_STYLE.md
parentdb2aef5a1d95cea835d2a76db6da266cc4af61d6 (diff)
downloadsystemd-9214f2999b85533ad184e6508f0a0a078d9d3b8f.tar.gz
CODING_STYLE: allow joined variable declarations and function calls
… but only for a single variable. The guidelines already allowed declaring variables at the point of initialization. But not making a function call to do that. Let's allow that now. The existing style of declaring and initializing seperate is still allowed, and whatever makes most sense should be used.
Diffstat (limited to 'docs/CODING_STYLE.md')
-rw-r--r--docs/CODING_STYLE.md27
1 files changed, 18 insertions, 9 deletions
diff --git a/docs/CODING_STYLE.md b/docs/CODING_STYLE.md
index f2c877cceb..4792f270c5 100644
--- a/docs/CODING_STYLE.md
+++ b/docs/CODING_STYLE.md
@@ -153,25 +153,34 @@ SPDX-License-Identifier: LGPL-2.1-or-later
## Using C Constructs
- 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.
+ the point where they can be initialized. Avoid huge variable declaration
+ lists at the top of the function.
+
+ As an exception, `r` is typically used for a local state variable, but should
+ almost always be declared as the last variable at the top of the function.
```c
{
- uint64_t a, b;
+ uint64_t a;
int r;
- a = frobnicate();
- b = a + 5;
+ r = frobnicate(&a);
+ if (r < 0)
+ …
+
+ uint64_t b = a + 1, c;
- r = do_something();
+ r = foobarify(a, b, &c);
if (r < 0)
+
+ const char *pretty = prettify(a, b, c);
+ …
}
```
-- Do not mix function invocations with variable definitions in one line.
+- Do not mix multiple variable definitions with function invocations or
+ complicated expressions:
```c
{
@@ -225,7 +234,7 @@ SPDX-License-Identifier: LGPL-2.1-or-later
- To determine the length of a constant string `"foo"`, don't bother with
`sizeof("foo")-1`, please use `strlen()` instead (both gcc and clang optimize
the call away for fixed strings). The only exception is when declaring an
- array. In that case use STRLEN, which evaluates to a static constant and
+ array. In that case use `STRLEN()`, which evaluates to a static constant and
doesn't force the compiler to create a VLA.
- Please use C's downgrade-to-bool feature only for expressions that are