coding style of linux kernel

92
Coding Style in Linux kernel Peter Chang 2012/06/17

Upload: peter-chang

Post on 19-May-2015

2.954 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Coding style of Linux Kernel

Coding Style in Linux kernelPeter Chang2012/06/17

Page 3: Coding style of 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.”~ Linus Torvalds

Page 4: Coding style of Linux Kernel

Indentation

Page 5: Coding style of Linux Kernel

Indentation

All tabs are 8 characters.

Use tabs.

If your code need 3 layers of indentation, it’s your problem. Solve it!

Page 6: Coding style of Linux Kernel

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.

Page 7: Coding style of Linux Kernel

Indentation

Put switch and case into the same layer of indentation

Page 8: Coding style of Linux Kernel

Indentation

Page 9: Coding style of Linux Kernel

Indentation

Good examples:

fs/*

kernel/*

Bad examples:

drivers/scsi/sg.c

Page 10: Coding style of Linux Kernel

Break long lines & strings

Page 11: Coding style of Linux Kernel

Break long lines & strings

There are only 80 char. in a line.Because we hate this:

Page 12: Coding style of Linux Kernel

Break long lines & strings

Page 13: Coding style of Linux Kernel

Braces

Page 14: Coding style of Linux Kernel

Braces

Page 15: Coding style of Linux Kernel

Braces

Page 16: Coding style of Linux Kernel

Braces

Page 17: Coding style of Linux Kernel

Braces

Page 18: Coding style of Linux Kernel

Braces

Page 19: Coding style of Linux Kernel

Braces

Page 20: Coding style of Linux Kernel

Braces

Page 21: Coding style of Linux Kernel

Braces

Good examples:

drivers/scsi/qla1280.c

kernel/*.c

Bad examples:

fs/devfs/*

Page 22: Coding style of Linux Kernel

Spaces

Page 23: Coding style of Linux Kernel

Spaces

“To be, or not to be”

Page 24: Coding style of Linux Kernel

Spaces

Use a space after these keywords:

if, switch, case, for, do, while

Do not add spaces after these keywords:

sizeof, typeof, alignof, __attribute__

Page 25: Coding style of Linux Kernel

Spaces

Good example:

s = sizeof(struct file);

Suck example:

s=sizeof( struct file );

Page 26: Coding style of Linux Kernel

Spaces

Add a space before and after the following binary and ternary operators:

= , + , - , * , /

< , > , % , & , | , ^

<= , >= , == , != , ? , :

Page 27: Coding style of Linux Kernel

Spaces

DO NOT Add a space after the following operators:

&, *, +, -, ~, !, sizeof, typeof, alignof, __attribute__, define

Page 28: Coding style of Linux Kernel

Spaces

DO NOT add a space before and after the following operators:

++,--

Page 29: Coding style of Linux Kernel

Spaces

DO NOT add a space before and after the following operators:

.

->

Page 30: Coding style of Linux Kernel

Naming

Page 31: Coding style of Linux Kernel

Naming

Be descriptive

Be concise

Good example:

void enable_mem_mailbox();

Page 32: Coding style of Linux Kernel

Naming

No MiXedCaSe

Bad examples:

int CommandAllocationGroupSize;

void DAC960_V1_EnableMemoryMailboxInterface();

Page 33: Coding style of Linux Kernel

Naming

Global variable should be use only if they are absolutely necessary.

Page 34: Coding style of Linux Kernel

Naming

Local variable should be short and the point

Good example:

i,j ( as a counter for a loop )

Bad example:

loop_counter

Page 35: Coding style of Linux Kernel

Naming

Don’t encoding the type of a function into the name (a.k.a. Hungarian notation).

“It’s brain damaged” ~ Linus Torvalds

Page 36: Coding style of Linux Kernel

Functions

Page 37: Coding style of Linux Kernel

Functions

Do one thing, and do it well

Short, one or two screens of text

Page 38: Coding style of Linux Kernel

Functions

Okay to have longer function doing small different things

Page 39: Coding style of Linux Kernel

Functions

“如果你寫出了⼀一個很複雜的function,表示你可能程度比高中⼀一年級的學生還差,因為你不會用function”~ Linus Torvalds

Page 40: Coding style of Linux Kernel

Functions

If more than 10 local variables, it’s too complex

Page 41: Coding style of Linux Kernel

Functions

“人最多同時只能記7件不同的事情。如果你覺得你是天才,那或許你兩個星期後就還看的懂你那複雜的函式”~ Linus Torvalds

Page 42: Coding style of Linux Kernel

Functions

Separate different functions with a blank line

If your function will be exported, use EXPORT* macro

Page 43: Coding style of Linux Kernel

Functions

Page 44: Coding style of Linux Kernel

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

Page 45: Coding style of Linux Kernel

GOTOCentralize exiting of functions

Page 46: Coding style of Linux Kernel

被封印的GOTO

“危險,不要用”

~ 好像小時候聽大人講過

“下⼀一節是要講Goto,但我們跳過,你們寫程式也不要用Goto”

~ 好像當初上大⼀一程設上課有講過

Page 47: Coding style of Linux Kernel

只是歷史遺跡?

組合語言發展過來的遺跡?

jump?

branch?

Page 48: Coding style of Linux Kernel

難道GOTO錯了嗎?

Page 49: Coding style of Linux Kernel

As the matter of fact, ...

The equivalent to GOTO statement is used frequently by compiler.

For what?

Unconditional jump instruction

Page 50: Coding style of Linux Kernel

Timing of using GOTO

Let function from multiple exit to only one exit

Page 51: Coding style of Linux Kernel

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 ;)

Page 52: Coding style of Linux Kernel

Example of using GOTO

Page 53: Coding style of Linux Kernel

Comment

Page 54: Coding style of Linux Kernel

Comments

Bad comments

explain how code works

say who wrote this function

have last modified date

have other trivial things

Page 55: Coding style of Linux Kernel

Comments

Good comments

explain what

explain why

should be at the beginning of function

Page 56: Coding style of Linux Kernel

Comments

Do not use:

// statement of comment ( C99-style )

Do use:

/* statement of comment ( C89-style) */

Page 57: Coding style of Linux Kernel

Comments

Page 58: Coding style of Linux Kernel

Comments

Page 59: Coding style of Linux Kernel

Comments

Page 60: Coding style of Linux Kernel

Kconfig

Page 61: Coding style of Linux Kernel

Indentation of Kconfig

Lines under a "config" definition are indented with one tab

help text is indented an additional two spaces

Page 62: Coding style of Linux Kernel

Indentation of Kconfig

Page 63: Coding style of Linux Kernel

Unstable Features in Kconfig

Features that might still be considered unstable should be defined as dependent on "EXPERIMENTAL"

Page 64: Coding style of Linux Kernel

Unstable Features in Kconfig

Page 65: Coding style of Linux Kernel

Dangerous feature in Kconfig

seriously dangerous features should advertise this prominently in their prompt string

Page 66: Coding style of Linux Kernel

Dangerous feature in Kconfig

Page 67: Coding style of Linux Kernel

Macro, enum, RTL

Page 68: Coding style of Linux Kernel

CAPITALIZE

Names of macros defining constants

labels in enums

For defining several related constants

Page 69: Coding style of Linux Kernel

CAPITALIZE

Example:

#define CONSTANT 0xffff0000

Page 70: Coding style of Linux Kernel

LOOOOONG Macro

Macros with multiple statements should be enclosed in a do - while block

Page 71: Coding style of Linux Kernel

LOOOOONG Macro

Page 72: Coding style of Linux Kernel

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.

Page 73: Coding style of Linux Kernel

Don’t use macro in these cases

Page 74: Coding style of Linux Kernel

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.

Page 75: Coding style of Linux Kernel

Don’t use macro in these cases

Example for 2:

#define FOO(val) bar(index, val)

Page 76: Coding style of Linux Kernel

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

Page 77: Coding style of Linux Kernel

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.

Page 78: Coding style of Linux Kernel

Don’t use macro in these cases

Page 79: Coding style of Linux Kernel

Allocate memory

Page 80: Coding style of Linux Kernel

Function return value and names

Page 81: Coding style of Linux Kernel

Unwritten rules

Page 82: Coding style of Linux Kernel

Unwritten rules

Use code that is already present

string

byte order functions

linked lists

Page 83: Coding style of Linux Kernel

typedef is evil

Page 84: Coding style of Linux Kernel
Page 85: Coding style of Linux Kernel

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?

Page 86: Coding style of Linux Kernel

Well, mostly...

Base system types

list_t

u8, u16, u64

Function pointer

Page 87: Coding style of Linux Kernel

No #ifdef in .c files

#ifdef belongs in .h file

Let your compiler do its work

Page 88: Coding style of Linux Kernel

Labeled elements in Initializers

Page 89: Coding style of Linux Kernel

Labeled elements in Initializers

Page 90: Coding style of Linux Kernel

Labeled elements in Initializers

Page 91: Coding style of Linux Kernel

Labeled elements in Initializers

Page 92: Coding style of Linux Kernel

Labeled elements in Initializers