Saturday, October 11, 2008

Coding Style...

No technical blog can start without commenting on the coding style. 90% of the times the code written by you would have to be understood by a third person who will enhance or maintain it. (The other 10% being college assignments.. even there the code has to be understood by MANY third people :P). So, wouldn't it be nice if your code was easily understandable and readable?

You will understand the importance of coding style once you start studying code written by others. Some get so frustrated that they re-indent the entire code base (:P) which leads to worse problems!! :P. So, better write understandable code. Coding is an art and an art is nothing without presentation!

A very good guide to coding style can be found in the linux kernel documentation.
Just check out Documentation/CodingStyle in the Linux kernel sources. Remember that this coding style is recommended by people who have written/maintained millions of LOC. It has to be the best. And it is!

The file also provides a configuration to be integrated with emacs so that emacs does some of the things automatically for you. Emacs is great!

Welcome to the world where we like to speak quality C !! :)

10 comments:

CDK said...

so the first post has landed...entire code base !! lol .... taking potshots here as well :P :D

Nachiket said...

Write a comparison of emacs vi and others in the next post.. will you ??

Praj ~ said...

too bad... I thought, from post heading, its about styling the webpages... or coding CSS style sheets :)

i dnt care indentation... aptana studio takes care of it :)

Jitesh Shah said...

@cdk
lol..no potshots :P .. just reminding someone of his/her horrible times :P

@nachiket
ok! sure!

@prajwalit
indentation is not even 1% of the coding style. Have a look at the documentation. You'll know!
Also, I am sure there are differences is coding style here too (the function names you give, etc etc). reconciling them is essential!

What's in the name said...

POSIX and ISO C standards deserve a mention and adherence.

Jitesh Shah said...

@saurabh
You get me wrong. I am not talking about C standards, I am talking about coding style.

It don't matter which standard you use. POSIX, ANSI or ISO. Coding style is how you present your code!

Vedang said...

coding style != indentation
go jitesh! proper discourse on coding style needed!

Pallavi said...

"The other 10% being college assignments.. even there the code has to be understood by MANY third people :P"

=))..tomne!!

..seriously pann..barobar ahe :)

Jitesh Shah said...

What is coding style?

1) Indentation.
2) How you layout your elements. eg. "for () {
}" is one style and
"for ()
{
}" is another. Former one is better.

3) What names you give to functions and variables.
4) How do you divide your logic into functions
5) How do you use typedefs and macros
eg. Do you write "tmp = a + 30/40" or do you write "tmp = a + SOMEMACRO"

6) Some good practices like, single exit of a function, using goto is NOT bad, etc etc

7) What values do your functions return (eg. 0 if successful, non-zero if error)

8) How do you use global variables

and the list goes on.... Coding Style is a truly vast topic and doesn't confine to just indentation...

Praj ~ said...

if you are adding all this in coding style is there anything remaining for actual programming thingy :)