Based on article by Greg Kroah-Hartman
I want to read your code.
I want you to read my code.
I want you to fix my bugs.
I want you to change my code.
I want to build on your code.
Lets people focus on substance, not style.
We need productive kernel programmers!
Use tabs.
All tabs are 8 characters.
If your code is indenting too deeply, fix the code.
Bad:
for(i=0; i<SIZE-1; i++) {
c=A[k=i];
for(j=i+1; j<SIZE; j++)
if(c>A[j])
c=A[k=j];
A[k] = A[i];
A[i] = c;
}
Bad:
function() { ... for(i=0; i<SIZE; i++) if(A[i]>0) for(j=i+1; j<SIZE; j++) if(A[j]>A[i]) for(k=i;... ... }
Good:
for(i=0; i<SIZE; i++) {
A[i] = random()%MAX;
printf("%d%c",A[i],i==SIZE-1?'\n':' ');
}
for(i=0; i<SIZE-1; i++) {
c=A[k=i];
for(j=i+1; j<SIZE; j++)
if(c>A[j])
c=A[k=j];
A[k] = A[i];
A[i] = c;
}
for(i=0; i<SIZE; i++)
printf("%d%c",A[i],i==SIZE-1?'\n':' ');
if (x is true) { we do y }
int function(int x) { body of function }
They have a lot of options, and especially when it comes to comment re-formatting you may want to take a look at the man page. But remember: it's not a fix for bad programming.
Bad:
CommandAllocationGroupSize
DAC960_V1_EnableMemoryMailboxInterface()
loop_counter
Good:
cmd_group_size
enable_mem_mailbox()
i
Good to have, but must be done correctly.
Bad comments:
explain how code works
say who wrote the function
have last modified date
have other trivial things
Good comments:
explain what
explain why
should be at beginning of function
Use code that is already present. In particulary:
It hides the real type of the variable.
Allows programmers to get into trouble:
large structures on the stack
large structures passed as return values
Can hide long structure definitions:
pick a better name
typedef just to signify a pointer type?
could you be lazier?
Base system types
u8, u16, u32, u64, etc.
dev_t
list_t
Function pointers
A "non-obvious" value that is hard coded
Fortunately, not many instances