Successfully reported this slideshow.
Your SlideShare is downloading. ×

Coding style of Linux Kernel

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
JavaScript: Core Part
JavaScript: Core Part
Loading in …3
×

Check these out next

1 of 92 Ad

More Related Content

Slideshows for you (20)

Similar to Coding style of Linux Kernel (20)

Advertisement

Recently uploaded (20)

Coding style of Linux Kernel

  1. 1. Coding Style in Linux kernel Peter Chang 2012/06/17
  2. 2. Reference “Linux Kernel Coding Style”, Linus Torvals, https:// computing.llnl.gov/linux/slurm/coding_style.pdf “Documentation / Coding-Style”, Greg Kroah- Hartman, IBM, Linux Symposium 2002 (Slide version: http://www.kroah.com/linux/ talks/ols_2002_kernel_codingstyle_talk/html/ index.html) http://www.kernel.org/doc/Documentation/ CodingStyle
  3. 3. “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.” ~ Linus Torvalds
  4. 4. Indentation
  5. 5. Indentation All tabs are 8 characters. Use tabs. If your code need 3 layers of indentation, it’s your problem. Solve it!
  6. 6. Indentation Don’t put two statement in the same line. Use a good editor. Don’t put andy empty line in the end of any file.
  7. 7. Indentation Put switch and case into the same layer of indentation
  8. 8. Indentation
  9. 9. Indentation Good examples: fs/* kernel/* Bad examples: drivers/scsi/sg.c
  10. 10. Break long lines & strings
  11. 11. Break long lines & strings There are only 80 char. in a line. Because we hate this:
  12. 12. Break long lines & strings
  13. 13. Braces
  14. 14. Braces
  15. 15. Braces
  16. 16. Braces
  17. 17. Braces
  18. 18. Braces
  19. 19. Braces
  20. 20. Braces
  21. 21. Braces Good examples: drivers/scsi/qla1280.c kernel/*.c Bad examples: fs/devfs/*
  22. 22. Spaces
  23. 23. Spaces “To be, or not to be”
  24. 24. Spaces Use a space after these keywords: if, switch, case, for, do, while Do not add spaces after these keywords: sizeof, typeof, alignof, __attribute__
  25. 25. Spaces Good example: s = sizeof(struct file); Suck example: s=sizeof( struct file );
  26. 26. Spaces Add a space before and after the following binary and ternary operators: =,+,-,*,/ <,>,%,&,|,^ <= , >= , == , != , ? , :
  27. 27. Spaces DO NOT Add a space after the following operators: &, *, +, -, ~, !, sizeof, typeof, alignof, __attribute__, define
  28. 28. Spaces DO NOT add a space before and after the following operators: ++,--
  29. 29. Spaces DO NOT add a space before and after the following operators: . ->
  30. 30. Naming
  31. 31. Naming Be descriptive Be concise Good example: void enable_mem_mailbox();
  32. 32. Naming No MiXedCaSe Bad examples: int CommandAllocationGroupSize; void DAC960_V1_EnableMemoryMailboxInterfa ce();
  33. 33. Naming Global variable should be use only if they are absolutely necessary.
  34. 34. Naming Local variable should be short and the point Good example: i,j ( as a counter for a loop ) Bad example: loop_counter
  35. 35. Naming Don’t encoding the type of a function into the name (a.k.a. Hungarian notation). “It’s brain damaged” ~ Linus Torvalds
  36. 36. Functions
  37. 37. Functions Do one thing, and do it well Short, one or two screens of text
  38. 38. Functions Okay to have longer function doing small different things
  39. 39. Functions “如果你寫出了⼀一個很複雜的function,表示你 可能程度比高中⼀一年級的學生還差,因為你不 會用function”~ Linus Torvalds
  40. 40. Functions If more than 10 local variables, it’s too complex
  41. 41. Functions “人最多同時只能記7件不同的事情。如果你覺 得你是天才,那或許你兩個星期後就還看的懂 你那複雜的函式”~ Linus Torvalds
  42. 42. Functions Separate different functions with a blank line If your function will be exported, use EXPORT* macro
  43. 43. Functions
  44. 44. Indentation Good examples: fs/*.c Bad examples: drivers/hotplug/ibmphp_res.c include WTF 370 lines drivers/usb/serial/usbserials.c use WTF 21 local variables
  45. 45. GOTO Centralize exiting of functions
  46. 46. 被封印的GOTO “危險,不要用” ~ 好像小時候聽大人講過 “下⼀一節是要講Goto,但我們跳過,你們寫程式 也不要用Goto” ~ 好像當初上大⼀一程設上課有講過
  47. 47. 只是歷史遺跡? 組合語言發展過來的遺跡? jump? branch?
  48. 48. 難道GOTO錯了嗎?
  49. 49. As the matter of fact, ... The equivalent to GOTO statement is used frequently by compiler. For what? Unconditional jump instruction
  50. 50. Timing of using GOTO Let function from multiple exit to only one exit
  51. 51. Reason of using GOTO Unconditional statements are easier to understand and follow nested is reduced!!! errors by not updating individual exit points when making modifications are prevented saves the compiler work to optimize redundant code away ;)
  52. 52. Example of using GOTO
  53. 53. Comment
  54. 54. Comments Bad comments explain how code works say who wrote this function have last modified date have other trivial things
  55. 55. Comments Good comments explain what explain why should be at the beginning of function
  56. 56. Comments Do not use: / statement of comment ( C99-style ) / Do use: /* statement of comment ( C89-style) */
  57. 57. Comments
  58. 58. Comments
  59. 59. Comments
  60. 60. Kconfig
  61. 61. Indentation of Kconfig Lines under a "config" definition are indented with one tab help text is indented an additional two spaces
  62. 62. Indentation of Kconfig
  63. 63. Unstable Features in Kconfig Features that might still be considered unstable should be defined as dependent on "EXPERIMENTAL "
  64. 64. Unstable Features in Kconfig
  65. 65. Dangerous feature in Kconfig seriously dangerous features should advertise this prominently in their prompt string
  66. 66. Dangerous feature in Kconfig
  67. 67. Macro, enum, RTL
  68. 68. CAPITALIZE Names of macros defining constants labels in enums For defining several related constants
  69. 69. CAPITALIZE Example: #define CONSTANT 0xffff0000
  70. 70. LOOOOONG Macro Macros with multiple statements should be enclosed in a do - while block
  71. 71. LOOOOONG Macro
  72. 72. Don’t use macro in these cases 1. macros that affect control flow It looks like a function call but exits the "calling" function; don't break the internal parsers of those who will read the code.
  73. 73. Don’t use macro in these cases
  74. 74. Don’t use macro in these cases 2. macros that depend on having a local variable with a magic name might look like a good thing, but it's confusing as hell when one reads the code and it's prone to breakage from seemingly innocent changes.
  75. 75. Don’t use macro in these cases Example for 2: #define FOO(val) bar(index, val)
  76. 76. Don’t use macro in these cases 3. macros with arguments that are used as l-values Ex: FOO(x) = y; will bite you if somebody e.g. turns FOO into an inline function
  77. 77. Don’t use macro in these cases 4. forgetting about precedence macros defining constants using expressions must enclose the expression in parentheses. Beware of similar issues with macros using parameters.
  78. 78. Don’t use macro in these cases
  79. 79. Allocate memory
  80. 80. Function return value and names
  81. 81. Unwritten rules
  82. 82. Unwritten rules Use code that is already present string byte order functions linked lists
  83. 83. typedef is evil
  84. 84. EVIL! EVIL! EVIL! It hides the real type of the variable Allows programmers to get into trouble large struct. on the stack large struct. passed as return value Can hid e long structure definition pick a better name typedef just signify a pointer type could you be lazier?
  85. 85. Well, mostly... Base system types list_t u8, u16, u64 Function pointer
  86. 86. No #ifdef in .c files #ifdef belongs in .h file Let your compiler do its work
  87. 87. Labeled elements in Initializers
  88. 88. Labeled elements in Initializers
  89. 89. Labeled elements in Initializers
  90. 90. Labeled elements in Initializers
  91. 91. Labeled elements in Initializers

×