r/C_Programming • u/yurtrimu • Jun 02 '26
What do you think about the linux kernel coding style?
https://www.kernel.org/doc/html/v4.10/process/coding-style.htmlLooks like a solid ruleset to follow in order to have consistent conventions all over the code you write. What do you guys think?
88
Upvotes
87
u/dmills_00 Jun 02 '26
In truth, while I have my preferences, ANY remotely sane coding style is so much better then none....
Ideally I like something where I can have astyle or similar run with a defined set of parameters to make things consistent on checkin, but other then that pick a common one, kernel is fine, so is K&R, whatever, just as long as it doesn't involve 90s style MS Hungarian.
39
u/greenfoxlight Jun 02 '26
I think in the end it does not matter much, beyond personal preference. Any guide is better than none.
I have gone back and forth on many points raised in the guide, but my current disagreements are:
But overall, it‘s a solid set of rules. My disagreements are pretty minor.
19
u/Drach88 Jun 02 '26 edited Jun 02 '26
I mostly like the Kernel style. You could do worse. Yes, formalized coding styles are a great idea, but be flexible. If you're working on or extending a codebase that other people have authored, use the coding style that they use.
For your own personal projects, pick a style that works for you, and if your preferences change later, use a different or modified style for those projects when you begin them.
One of the prescribed formats for the Kernel style guide is to not use braces for single statement branches. I personally hate this, and will always use braces. ie I prefer:
Instead of:
I'm also a heretical 4-space indenter.
It's a personal preference, so I use it on all of my own codebases, unless someone else's pre-existing style supercedes my own.
Otherwise, I have no major qualms with the Kernel style, and it's very much how I like to code.
11
u/AKostur Jun 02 '26
Create a .clang-format in your project. Use it consistently. Create any other guidelines that you feel you need. That it may or may not be used in the linux kernel is completely irrelevant. Do I agree with everything in the kernel code style guide? Nope. Does it matter that I disagee: also nope.
2
u/max123246 Jun 03 '26
Wait is this an official coding style for the kernel? It's quite...inflammatory and unprofessional
4
u/SecretlyAPug Jun 02 '26
i agree with or understand basically all of it except for the naming. yes, names should be laconic, but i never understood using names that aren't full words.
for their example, i would name a temporary counter variable `counter`; it's simple, short, but still obvious what it does. `tmp` tells you nothing about what the variable does and is only a few characters shorter.
additionally, abbreviations are subjective. even a word like "temporary" can be divisive (if i really had to abbreviate it, i'd use `temp`, not `tmp`). sticking to full words avoids that inconsistency.
2
u/AdreKiseque Jun 02 '26
It's certainly a coding style
2
u/questron64 Jun 02 '26
That's fine. Whatever. Just be consistent and reasonable.
2
u/collectgarbage Jun 03 '26
Whenever I get pulled up for not using the coding style:
“Parlay! I invoke the right of parlay. According to the Code of the Brethren, set down by the coding pirates Kernighan and Ritchie, you have to take me to your Captain”
2
u/NukiWolf2 Jun 03 '26
Talking about readability and then demanding 8 spaces for indentation and line breaks at 80 characters is just crazy. I have no problem with code that uses 2 spaces for indentation, but 8 spaces in combination with a maximum line length of 80 characters is in my opinion the opposite of ensuring readability and just enforces writing code that satisfies the coding style instead of writing code that is easy to understand.
4
u/T-J_H Jun 02 '26 edited Jun 02 '26
Seems okay. I personally am one of the heretical 2 space indenters, and I feel like opening braces on a new line are a waste of screen real estate. And I’ve never really gotten why the pointer * would go with the name instead of the type, but alas that’s what most sane styles do.
In the end, most important to me are to have clear descriptive naming - but not overly say. Nowadays with the IDEs we have, with autocomplete there’s no need to abbreviate into obscure territory, but on the other hand type suffixes are not needed either due to intellisense and the like. Stuff like p_ for parameters isn’t needed eigher, the IDE can display them differently anyways (in italic or such)
1
u/nukem996 Jun 02 '26
What you pointed to is the base Linux kernel subset. Each subsystem can have its own rules added on top of that. The main kernel tool for lint checking, checkpatch.pl only fully supports the main coding style, not each subsystem.
Documentation varies between each subsystems coding standards. It mostly comes out during review where your patchset is rejected for not following the standard. Alot of the rules are fairly arbitrary such as
* Reverse x-mas tree notation
* GPL vs BSD functions signatures
* White space usage
* Common variable names
I like the standard in general I just wish it was consistent and there was a tool I could rely on for compliance.
1
u/DenseTemperature6135 Jun 03 '26
I think it might work.
1
u/Kokowaaah Jun 03 '26
Any consistent coding style that can be automated and enforced with a formatter (e.g. clang-format). Not everything can be checked properly though (naming is hard).
It is great to have discipline, better yet if it is checked by pre-commit/CI.
1
u/etaithespeedcuber Jun 03 '26
Some disgusting codebases do this:
And I just want to point out that if you enjoy coding like this i find you sociopathic
1
u/imdibene Jun 03 '26
It’s good enough, and the most important part is consistency throughout the whole codebase
1
u/Interesting_Debate57 Jun 03 '26
The bulk of what I find hard to read in other people's code can be fixed by having a common config file for whatever IDE you code in.
A surprising amount of mostly typographic things are configurable, for instance, in the jetbrains IDEs.
1
u/Life_Ad_369 Jun 05 '26
I hardly digress with the curly pracess one how can you bear the inconsistency.
1
u/chrism239 Jun 03 '26
Obligatory clip from Silicon Valley - https://www.tiktok.com/@cryptoknight614/video/7124785018739936558
0
u/capilot Jun 03 '26
Wait … there's a coding style?
0
u/smcameron Jun 03 '26 edited Jun 03 '26
Since I worked on open source linux drivers for more than a decade in a previous life, it's a habit for me now. I quite like it. Might be stockholm syndrome. But tabs over spaces forever! Use checkpatch.pl to keep yourself in line. I have integrated a variant of checkpatch.pl into a few of my projects.
-7
u/reini_urban Jun 02 '26
Not as good as the default clang-format style. Tab 8 leads to spaghetti and too many functions, as you cannot nest locally. This is fine for simple code as in a kernel, but unusable in more complicated SW
2
u/[deleted] Jun 02 '26
[removed] — view removed comment