TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Linux kernel coding style

124 pointsby sthlmover 10 years ago

15 comments

kagiaover 10 years ago
I doubt everyone agrees to that coding style, I certainly don't. However when submitting code to a project I'd still stick to the prescribed coding style, because I believe consistency in any code base can be just as important as any other measure of readability.
评论 #8662030 未加载
notacowardover 10 years ago
Mostly good advice, sometimes even great, but the part about typedefs is total BS. Any non-trivial program will use values that have clearly different meanings but end up being the same C integer type. One&#x27;s an index, one&#x27;s a length, one&#x27;s a repeat count, one&#x27;s an enumerated value (&quot;enum&quot; was added to the language to support this very usage), and so on. It&#x27;s stupid that C compilers don&#x27;t distinguish between any two types that are the same width and signedness; why compound that stupidity? Both humans and static analyzers could tell the difference if you used typedefs, and avoid quite a few bugs as a result. Being able to change one type easily might also make some future maintainer&#x27;s life much better. There&#x27;s practically no downside except for having to look up the type in some situations (to see what printf format specifier to use), but that&#x27;s a trivial problem compared to those that can result from <i>not</i> using a typedef.<p>Don&#x27;t want to use typedefs? I think that&#x27;s a missed opportunity, but OK. Don&#x27;t use them. OTOH, anyone who tries to pretend that the bad outweighs the good, or discourage <i>others</i> from using them, is an ass. Such advice for the kernel is even hypocritical, when that code uses size_t and off_t and many others quite liberally.
评论 #8662264 未加载
评论 #8662404 未加载
评论 #8662270 未加载
评论 #8663646 未加载
评论 #8662235 未加载
评论 #8662422 未加载
robinhoodexeover 10 years ago
&quot;First off, I&#x27;d suggest printing out a copy of the GNU coding standards, and NOT read it. Burn them, it&#x27;s a great symbolic gesture.&quot;<p>Shots fired.
评论 #8662081 未加载
评论 #8662051 未加载
patrickgover 10 years ago
I am glad that for Go there is `go fmt` which predefines some of the issues mentioned in the article. Thus there is &quot;one global coding style for Go&quot;. It&#x27;s another matter if one likes it or not.
评论 #8662065 未加载
jackalopeover 10 years ago
&quot;Get a decent editor and don&#x27;t leave whitespace at the end of lines.&quot;<p>Trailing whitespace always raises a huge red flag for me whenever I look at someone&#x27;s code. It&#x27;s not just sloppy, it often makes diff output so noisy you can&#x27;t detect real changes to the code.
评论 #8663540 未加载
评论 #8662773 未加载
JBiserkovover 10 years ago
&gt;Encoding the type of a function into the name (so-called Hungarian notation) is brain damaged - the compiler knows the types anyway and can check those, and it only confuses the programmer. No wonder MicroSoft makes buggy programs.<p>&quot;Making Wrong Code Look Wrong&quot; by Joel Spolsky is a must-read and contains an explanation of Apps Hungarian (the original, thoughtful one) vs Systems Hungarian <a href="http://www.joelonsoftware.com/articles/Wrong.html" rel="nofollow">http:&#x2F;&#x2F;www.joelonsoftware.com&#x2F;articles&#x2F;Wrong.html</a>
评论 #8662258 未加载
评论 #8662156 未加载
kakakikiover 10 years ago
&quot;There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.&quot;<p>HA!
评论 #8662011 未加载
kijinover 10 years ago
&gt; <i>spaces are never used for indentation</i><p>If indentation should always use tabs (0x09) and never spaces (0x20), then the whole rant about indentation width is pointless. Any modern editor will allow you to adjust the displayed width of a tab. It&#x27;s only when you use spaces for indentation that the width becomes a concern.
评论 #8662112 未加载
评论 #8662105 未加载
raverbashingover 10 years ago
I like it<p>I really prefer using tabs. Having it displayed as 8 spaces in other languages is not as good as in C<p>And they get it right about typedefs in C
评论 #8662076 未加载
评论 #8662203 未加载
BugsBunnySanover 10 years ago
It&#x27;s a very nice coding style. It keeps the code in pieces that are easy to grasp as units, it doesn&#x27;t waste space and doesn&#x27;t clutter the code at the same time.<p>Just take any random function from the kernel sources and ask yourself, what does it do. I think in most cases you&#x27;ll find it&#x27;s really obvious...<p>For me I find the kernel sources one of the most readable and understandable sources I&#x27;ve seen. The structure of them is just so clearly visible from the sources. I think a lot of that has to do with the coding style.
davidwover 10 years ago
I&#x27;m a fan of the Tcl&#x2F;Apache&#x2F;BSD style. Indeed, Tcl has nice C code:<p><a href="https://github.com/tcltk/tcl/blob/master/generic/tclCompile.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tcltk&#x2F;tcl&#x2F;blob&#x2F;master&#x2F;generic&#x2F;tclCompile....</a>
评论 #8662130 未加载
thatswrong0over 10 years ago
&gt; Do not unnecessarily use braces where a single statement will do.<p><pre><code> if (condition) action(); </code></pre> &gt; and<p><pre><code> if (condition) do_this(); else do_that(); </code></pre> The Apple SSL bug (<a href="https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/" rel="nofollow">https:&#x2F;&#x2F;nakedsecurity.sophos.com&#x2F;2014&#x2F;02&#x2F;24&#x2F;anatomy-of-a-got...</a>) makes me wonder if this is really worth the potential for introducing bugs.
评论 #8662606 未加载
评论 #8665596 未加载
whoisthemachineover 10 years ago
I agree with and already practice many of these conventions (at least the ones that apply to C-like languages in general). It&#x27;s interesting that I do and I kind of wonder what lead me down that path, since I haven&#x27;t programmed in C since my college days. I often think that my assembly class from those days pushed me into making my code as vertical as possible rather than the indented-if-statement-curly-brace hell that I often see, since assembly was very readable without having that capability.
geekamover 10 years ago
&gt;&gt; Do not unnecessarily use braces where a single statement will do.<p>Shouldn&#x27;t this be changed to always use braces? Given the Apple bug?
评论 #8663608 未加载
Nmachineover 10 years ago
I stopped reading pretty early on: ... if you need more than three levels of indentation you&#x27;re screwed and should rewrite...