close

 


         Linux Kernel Coding Style


         Linux Kernel程式設計規範


 


This is a short document describing the preferred coding style for the linux kernel.  Coding style is very personal, and I won't _force_ my views on anybody, but this is what goes for anything that I have to be able to maintain, and I'd prefer it for most other things too.  Please at least consider the points made here.


這是一篇描述linux kernel程式設計風格的短文程式寫作風格是因人而異的所以我不願意 _強迫_ 他人接受我的看法但是現在我們是討論一份我必須維護的程式碼而我也偏好在其他方面使用這種風格請你至少考慮這裡所作的論點


 


First off, I'd suggest printing out a copy of the GNU coding standards, and NOT read it.  Burn them, it's a great symbolic gesture.


首先我想建議你印一份GNU程式設計風格指南出來然後千萬不要讀它把它燒掉它實在是一篇充滿代號的咒文


 


Anyway, here goes:


總而言之下麵就讓我們開始吧


 


 




 


         Chapter 1: Indentation


         1: 縮排


 


Tabs are 8 characters, and thus indentations are also 8 characters. 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.


Tab就是8個字元所以縮排也就是8格。有個邪說想把縮排設成4 (甚至2) 這就像是嚐試把PI的值定義成3一樣。


 


Rationale: The whole idea behind indentation is to clearly define where a block of control starts and ends.  Especially when you've been looking at your screen for 20 straight hours, you'll find it a lot easier to see how the indentation works if you have large indentations.


原因:縮排背後的真正意義是用來清楚的界定一個控制區塊的開始與結束特別是當你已經盯了連續20小時的螢幕的時候你就會發現使用大縮排是如何有效的發揮它應有的功能


 


Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen.  The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.


到此為止有些人會提出縮8格會讓程式碼移到太右邊去使得在80個字寬的終端機螢幕下非常的難以閱讀我的回答是如果你需要多過3層的縮排無論如何你大概已經搞砸了應該要修改你的程式才對


 


In short, 8-char indents make things easier to read, and have the added benefit of warning you when you're nesting your functions too deep.


Heed that warning.


總而言之 8格的縮排較容易閱讀並且當你的副程式巢狀太深的話有個附加的預警效果要注意這個警告


 


The preferred way to ease multiple indentation levels in a switch statement is to align the "switch" and its subordinate "case" labels in the same column instead of "double-indenting" the "case" labels.  E.g.:


 


    switch (suffix) {


    case 'G':


    case 'g':


        mem <<= 30;


        break;


    case 'M':


    case 'm':


        mem <<= 20;


        break;


    case 'K':


    case 'k':


        mem <<= 10;


        /* fall through */


    default:


        break;


    }


 


 


Don't put multiple statements on a single line unless you have something to hide:


除非你需要隱藏一些東西,否則不要將多條語句放在同一行


 


 


    if (condition) do_this;


      do_something_everytime;


 


Don't put multiple assignments on a single line either.  Kernel coding style is super simple.  Avoid tricky expressions.


 


Outside of comments, documentation and except in Kconfig, spaces are never used for indentation, and the above example is deliberately broken.


另外,除了註釋,檔以及Kconfig檔,不應該使用空格來縮排,上面的例子是對這兩條規則的有意破壞。


 


Get a decent editor and don't leave whitespace at the end of lines.


最後,你需要找一個像樣的編輯器並且不要在行尾留下空格。


 


 




 


         Chapter 2: Breaking long lines and strings


         2: 打斷過長的程式行及字串


 


Coding style is all about readability and maintainability using commonly available tools.


指定編碼風格的主要作用就是增強代碼在常用工具中的可讀性和易維護性。


 


The limit on the length of lines is 80 columns and this is a strongly preferred limit.


因此,一行的長度應該嚴格限制在80字元以內,這是一條硬性規定。


 


Statements longer than 80 columns will be broken into sensible chunks. Descendants are always substantially shorter than the parent and are placed substantially to the right. The same applies to function headers with a long argument list. Long strings are as well broken into shorter strings. The only exception to this is where exceeding 80 columns significantly increases readability and does not hide information.


如果一條語句超過了80個字元,那麼應該將它分成多行,每一個子行的長度都不應該超過父行,並且子行應該相對於父行進行充分的向右縮格。這條規則同樣適用於函式參數過長的函式。過長的字串也應該被分割成多個較短的字串。


 


void fun(int a, int b, int c)


{


    if (condition)


        printk(KERN_WARNING "Warning this is a long printk with "


                        "3 parameters a: %u b: %u "


                        "c: %u \n", a, b, c);


    else


        next_statement;


}


 




. 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 立你斯 的頭像
    立你斯

    立你斯學習記錄

    立你斯 發表在 痞客邦 留言(0) 人氣()