Linux kernel coding style
01.org
01.org
https://github.com/systemd/systemd/blob/master/CODING_STYLE#...
I once found a particularly confusing use of this style here:
https://github.com/erlang/otp/blob/master/erts/epmd/src/epmd...
But it is also so easy to catch that automatically (and in fact, gcc and clang will flag this as an error for you with the right incantation, and refuse to compile), that it is stupid to leave it to 50 different committers all avoiding accidents.
Of course, as others have pointed out, with some sort of reasonable linting setup or with compiler warnings, you'll probably catch the bug (unless macros are involved), but even so, I feel having something that looks like a block but isn't, adds cognitive load for little benefit.
If you want to enforce codding style (which is OK) you need to provide developers the tools to ease that process.
Contributing with patches to 15 different projects, using 15 different rules for styling makes it impossible to keep up.
FOSS projects should start using tools like clang-format so developers don't need care if a project use 2/4/6/8 spaces instead of tab. The tool should be able to automatically format the code before committing.
Some projects even use git hooks to compare the commit with auto-generated styling tools to check if it follows the rules.
As a committer, I would rather have it clearly explained what the rules are before I do it incorrectly. Sure, having tools that will do it for me is awesome, but considering how many examples in the OP are different because of historical reasons, those tools are difficult to configure.
That's exactly what tools like clang-format fixes. Code will be in the repository using one correct styling. The difference is that the commiter won't have to style it himself.
> As a committer, I would rather have it clearly explained what the rules are before I do it incorrectly. Sure, having tools that will do it for me is awesome, but considering how many examples in the OP are different because of historical reasons, those tools are difficult to configure.
Which tools you said that are difficult to configure? clang-format? It's 5~20 lines in a .clang-format in the root folder of your repo. Configure it once and profit forever.
* I'm talking about clang-format here because that's what I use for C/C++. I don't know anything about other languages.
It's the same for any other mental load that could be offloaded to machine. Coding style? Don't demand people sticking to rules - use some formatter/prettyfier/linter. Well known and frequent bugs? At least try to use static analysis or write tests and run those with some sanitizer. Codebase too big/convoluted/... - use some tools (parsers, indexers, search) to get better insight. Just try use proper tools and machines.
I'm not suggesting unlimited but isn't it time we revisit this?
It really feels like one of those "we've always done it this way so just leave it"
This being HN, naturally someone's going to tell me they do most of their programming on a 7 inch terminal screen.
But 80 chars comes with a cost: over terse code, or a lot of scrolling.
Personally, I've started using 100 columns for personal projects. But I stick to 80 for anything someone else is going to have to look at. To some extent, its more important to have a standard, so you don't have to worry about weird line-wrapping placements when someone else looks at your code, than it is to have one that fits superwell on the average modern screen. So we're probably stuck with 80
I started programming C full time about 2.5 years ago (after many more with PHP, et al). There's an aesthetic to writing C that I don't find with other programming languages. I believe this is, in part, due to the generalized use of shorter variables and keeping everything as compact and simple as possible.
Since deciding to keep everything at an 80-char width, I can say that it has helped me maintain a certain kind of readability I don't find with other languages. And, while aesthetics don't matter once the compiler takes over, I can say that good aesthetics do improve my ability to program more effectively and efficiently.
Note that it isn't a hard limit. The actual rule is "[s]tatements longer than 80 columns will be broken into sensible chunks, unless exceeding 80 columns significantly increases readability and does not hide information." So longer than 80 is fine, but only if it significantly increases readability. That sounds about right to me!
myDescriptiveResultVariable = myDescriptiveMethod(myWordyVariable, myVerboseVariable);
That's 87 characters.
Most projects aren't that big and don't have needs that are like the Linux kernel. There may be (and probably are) good reasons not to adopt its idiosyncratic choices.
I also think that code that has been constrained to 80 columns reads better. Personal opinion of me.
TL;DR - Linux kernel source code use TABs (8 characters) instead of spaces. The rationale behind is that the maintainers believe that large indentation makes code easier to read on screen (especially for long hours), makes sense.
Personally I (not a programmer but Linux SysAdmin/Ops/Infra Architect background) tend to use 4 spaces everywhere else (e.g. Shell, Ruby, Java and all sorts of configuration files). Not to pick a fight (sounds familiar? ;-) but 2 spaces in general make readability worse.
Anyway, the most important point is to honour what is already established/in place and stick to it, whatever you work on.
I know, everyone is going to downvote me for saying this. But if you're literally using 8 spaces to represent a tab, which is the default width tabs are rendered at, then what downside could there be to switching to actual tabs?
Edit: "spaces are never used for indentation" so do they actually use tab characters then?
from the LLVM style guide.
Don't mix tabs and spaces in indentation btw. That is the one thing you must never do.
foo = 1;
this_value_is_not_foo = 8;
another_int = 419;
pi = 314159;
versus: foo = 1;
this_value_is_not_foo = 8;
another_int = 419;
pi = 314159;The important thing is that you don't mix tabs and spaces in either portion. So, in my proposal, the indentation portion would be all tabs, and the tabular alignment part would be all spaces.
Not every group of variable assignments warrants a comment for every line, nor is my argument relevant only to static constants, nor is there any reason why aligned assignments should be construed to be related any more than consecutive assignments should be, nor should that stop you from reaping the unarguable improved readability of aligning them anyway.
The true impression. If they're actually unrelated, they're more likely to be in different files outright, than immediately sequential. "Encapsulating" in a struct is pointless/potentially obfuscatory if they're related local temps - but I'll use the same style for initializing structs.
> And once you add the comments explaining what the first three variables are and how they should be used, and the comment explaining why pi doesn't have a decimal point, there is no benefit to alignment.
I align those comments too.
> If you add another long line, you have to realign the whole list
The one drawback.
Also, alignment like that produces an excessive amount of diff noise: when you add or remove an entry, every entry changes.
I absolutely agree that this is a drawback — no approach is without some flaw. This is the flaw in mine. That said, I'm utterly convinced that it is worth this drawback.
That and having a standard allows people to speak a common language.
Makes sense. I don't mind stick to established style, personally I'll still prefer 4 instead of 2 for readability (easy on eyes...).
On that subject, I really dislike some of the PHP PSR formatting guidelines, it is a C style language and they have implemented some really painful paradigms
What are they? I have found that PHP guidelines are surprisingly good.
Of course, these have little to do with core PHP, but PSR-1, PSR-0/4, and large portions of PSR-2 have been widely adopted within the PHP development community. The most-debated portion of PSR-2 is, of course, the use of 4 spaces for indentation (no tabs).
PSR-12 aims to bring the guidelines up to date with the latest features in PHP 7, but I've mostly lost track of things in PHP-land since moving to a new job and burying myself in XSL (and occasionally C#).
(There's a link to Sphinx at the bottom of the page)
It looks like this is just generated from Documentation/index.rst and linked pages in the kernel source tree, which seems to have been created less than a year ago: https://github.com/torvalds/linux/commits/master/Documentati...
There's also a copy on kernel. org, not sure why Intel's copy is being linked here. It's probably the same text, so any copy should be equally good, it just feels less canonical to me. Probably silliness on my part for even thinking of it.
> Built with Sphinx using a theme provided by Read the Docs.
John Corbert has done quite a bit recently within Documentation to make it more structured and has (from my understanding) massively revamped the documentation compilation.
The LibreSSL team spent months putting the OpenSSL code into KNF. It makes a big difference, even though it may seem trivial.
Should I be aware of something from GNU coding standards?
The madman!
/s
Actually, now that I think about it, he has a point. Having a little more horizontal space seems like it would be easier on the eyes.
I hate line length limits personally. Especially because I tend to use long identifiers which eat up most of my line length limit in one go if you try to utter them with their enclosing namespaces.
Having some such rough limit is a good thing, but the number 3 is language dependent. Java for example automatically eats one level for the class.
Also C lacks syntax such as nested functions, try blocks, python-style context managers and all kinds of other stuff which excuse more levels of indentation.