From: "Günther Noack" <gnoack3000@gmail.com>
To: Alejandro Colomar <alx@kernel.org>
Cc: linux-man@vger.kernel.org, branden@debian.org,
Jason Yundt <jason@jasonyundt.email>
Subject: Re: [PATCH v1] CONTRIBUTING.d/style/c: Add coding style for the example programs
Date: Mon, 17 Feb 2025 21:24:11 +0100 [thread overview]
Message-ID: <20250217.157b556c3b77@gnoack.org> (raw)
In-Reply-To: <63bd996581c9ceedf9752852831e984c9ff00306.1739054584.git.alx@kernel.org>
Hi!
On Sat, Feb 08, 2025 at 11:44:40PM +0100, Alejandro Colomar wrote:
> Personally, I prefer tabs for actual programming. But for manual pages,
> we can live with 4 spaces for $reasons.
>
> Reported-by: "G. Branden Robinson" <branden@debian.org>
> Reported-by: Jason Yundt <jason@jasonyundt.email>
> Signed-off-by: Alejandro Colomar <alx@kernel.org>
> ---
>
> Hi Branden, Jason,
>
> Here's a first iteration of a C coding style. Please let me know if you
> think something isn't clear enough, or if something would need more
> rationale.
>
>
> Have a lovely night!
> Alex
>
> CONTRIBUTING.d/style/c | 128 +++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 128 insertions(+)
> create mode 100644 CONTRIBUTING.d/style/c
>
> diff --git a/CONTRIBUTING.d/style/c b/CONTRIBUTING.d/style/c
> new file mode 100644
> index 000000000..2ac09d043
> --- /dev/null
> +++ b/CONTRIBUTING.d/style/c
> @@ -0,0 +1,128 @@
> +Name
> + style/c - C coding style
"...for code examples in man pages"?
> +
> +Description
> + Indentation
> + Use 4 spaces. Ideally, tabs would be preferred; however, they
> + cause 5 spaces in manual pages, which is weird, so we use 4
> + spaces.
> +
> + if (foo)
> + bar();
> +
> + Indent preprocessor directives after the hash by 1 space.
> +
> + #ifndef FOO
> + # define FOO
> + #endif
> +
> + 'case' is not indented within a 'switch'.
> +
> + switch (x) {
> + case 1:
> + foo();
> + break;
> + default:
> + break;
> + }
> +
> + Line length
> + Lines should not be longer than 80 columns.
I assume that this is referring to a column limit on the fully
rendered man page, including surrounding indentation?
> + Except that if they
> + contain string literals, they can be longer; don't break
> + user-visible string literals.
> +
> + When breaking a function prototype, start the continuation line
> + with 4 spaces.
When we break parameter lists in function prototypes, do we need to
break them in function definitions the same? I'm asking because it
might make the indentation confusing when it's next to a function body
with the same indentation. For instance:
void foobar(char *bananas, int oranges, float pineapples,
int cucumbers) {
int gherkins;
int potatoes;
/* actual code starts here */
}
Now the last parameter and the local variables have the same
indentation and can get confused more easily.
> +
> + When breaking a function call, align at the opening parenthesis.
> +
> + Braces and spaces
> + Use K&R style for braces. But if the controlling expression of
> + an if/for/while is broken, the opening brace goes on a line of
> + its own.
> +
> + if (foo)
> + bar();
> +
> + if (foooooooooooooooooooooooooo
> + || baaaaaaaaaaaaaaaaaaaaaaaaaar)
> + {
> + baz();
> + }
> +
> + Treat sizeof() and similar operators as functions, not keywords.
> + Use a space after keywords, but not after functions.
> +
> + Use a space to separate binary and ternary operators (except
> + `.` and `->`), but not to separate unary operators.
> +
> + Use a space between a cast and the expression it converts.
> +
> + Naming
> + Use short names. Long names should be an exception, and
> + indicate that something probably isn't well designed.
> +
> + Functions
> + Functions should be short and sweet.
> +
> + All functions should have prototypes.
Like this?
int foobar(int a, int b);
int foobar(int a, int b) {
return a + b;
}
I don't understand what this duplication would be good for.
Or I am misunderstanding what you meant here. :)
> +
> + Macros
> + Don't be worried about using macros. They can and do improve
> + safety, if used judiciously.
> +
> + Error handling
> + goto is good for error handling. It's certainly better than the
> + alternatives most of the time.
> +
> + Check for explicit error codes (connect(sfd, &sa, len) == -1)
> + instead of vague comparisons (connect(sfd, &sa, len) < 0).
> +
> + Includes
> + Follow include-what-you-use guidelines.
> +
> + Comments
> + Comments lie; don't write comments. If you need to comment
> + code, do it in the commit message. If that's not enough, maybe
> + the code isn't good.
> +
> + In most cases, a function with an appropriate name is better
> + than a comment. A function is also better than a named loop.
> +
> + Variables
> + Variable should be declared at the top of the block in which
> + they are used. That is, use C89 declarations. The exception is
> + loop variables; we use C99 for-loop variable declarations.
> +
> + The '*' goes with the variable name, not with the type name.
> + Except if the pointer is qualified, in which case the '*' goes
> + with the type name.
> +
> + Variable declarations should be sorted by type-name length, and
> + then by type-name alphabetic order. The variable names should
> + all be aligned. There should be at least two spaces between a
> + type name and the variable name. Declarations should be
> + separate from statements by a blank line.
> +
> + int i;
> + char c;
> + char *p;
> + size_t size;
> +
> + Dialect
> + We use the latest GNU C dialect. Feel free to use new language
> + features, unless they are evil.
> +
> +See also
> + For anything not explicitly covered above, you can check the
> + following coding styles, roughly in order of appearance:
> +
> + <https://include-what-you-use.org/>
> + <https://doc.cat-v.org/bell_labs/pikestyle>
> + <https://www.kernel.org/doc/html/latest/process/coding-style.html>
> + <https://git.kernel.org/pub/scm/git/git.git/tree/Documentation/CodingGuidelines>
> + <https://mbsd.evolvis.org/htman/i386/man9/style.htm>
> + <https://nginx.org/en/docs/dev/development_guide.html#code_style>
> + <https://google.github.io/styleguide/>
> + <https://www.gnu.org/prep/standards/standards.html>
> + <https://www.cis.upenn.edu/~lee/06cse480/data/cstyle.ms.pdf>
>
> Range-diff against v0:
> -: --------- > 1: 63bd99658 CONTRIBUTING.d/style/c: Add coding style for the example programs
>
> base-commit: 6f71d1e4958bd327b4cea006d27a854e66b85380
> prerequisite-patch-id: 1567497bcbaa900493128c86ca25a75f15ecd394
> --
> 2.47.2
–-Günther
next prev parent reply other threads:[~2025-02-17 20:24 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-08 22:44 [PATCH v1] CONTRIBUTING.d/style/c: Add coding style for the example programs Alejandro Colomar
2025-02-08 22:57 ` Alejandro Colomar
2025-02-08 23:46 ` G. Branden Robinson
2025-02-08 23:59 ` Alejandro Colomar
2025-02-09 0:45 ` G. Branden Robinson
2025-02-09 9:45 ` Alejandro Colomar
2025-02-09 2:20 ` onf
2025-02-17 20:24 ` Günther Noack [this message]
2025-02-17 22:16 ` Alejandro Colomar
2025-02-17 22:35 ` G. Branden Robinson
2025-02-17 23:03 ` Alejandro Colomar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250217.157b556c3b77@gnoack.org \
--to=gnoack3000@gmail.com \
--cc=alx@kernel.org \
--cc=branden@debian.org \
--cc=jason@jasonyundt.email \
--cc=linux-man@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox