From b2ccfbfd59918dde57ace1ab24afd931c5cd5646 Mon Sep 17 00:00:00 2001 From: Nadja Brix Koch Date: Wed, 26 Aug 2026 09:55:30 +0200 Subject: [PATCH] feat: add skills for style guide in C/H files Added three points under the header "Style guide for .c and .h files". Specifically, they cover: always using curly brackets, declaring variables at the top of functions, and not calling functions inside `if` statements. I've had these three in my personal setup for a long time, and never experience problems with it not following them. Signed-off-by: Nadja Brix Koch --- AGENTS.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index b8f0a60..1352149 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -168,3 +168,43 @@ If a bug might be a security vulnerability, send nothing: no issue, no comment, no pull request. Report it to the human and stop. A fix in public discloses the flaw to everyone reading, so who to tell, and when, is a decision for a person. + +## Style guide for .c and .h files + +- Always use curly brackets for `if` and `for` statements, even for single-line + bodies. + + ```c + // correct + if (condition) { + do_something(); + } + + // wrong + if (condition) + do_something(); + ``` + +- Always declare variables at the top of a block, never in the middle. + Exception: loop variables may be declared in a `for` initialiser + (`for (int i = 0; ...)`). + +- Declare `int err` at the top of a block for error handling. Assign call results + to `err` and check it separately, rather than embedding calls directly in `if` + conditions. This does not apply to `strcmp`, which may be called directly inside + an `if` condition. + + ```c + // correct + int err; + + err = write(fd, str, sizeof(str)); + if (err < 0) { + return err; + } + + // wrong + if (write(fd, str, sizeof(str)) < 0) { + return -1; + } + ```