Coding Style …

I have never thought much about coding style before i did my NOSIP in Novell. But once i started coding for ldtprecord, according to the coding style suggested to me by nags, i was surprised to see how nice and neat the final code looks.

Some tips/tricks for nice coding skills are,

1. Do spend some time to think about the variable names and the function names. This sometimes might be bit boring, especially when you want to concentrate much on the program logic and performance. But this is Rule 0 for coding conventions. A variable name “k” can imply anything like “kappa, kozhukattai, katthu, kaadhal, kerala, kozhuppu…” to someone who might have to read your code later. This is again mentioned here clearly. Many thanks to emacs, you can always use the auto complete, if your variable name is too long. 🙂 .
2. The actual coding convention depends much on the language and the standards your team is using already. The following style won’t work for someone, whose team is already using a totally different style.

A few examples for C is posted here .

Sample Code 1 :

if (a == 5) {

    b = 10;

}
else {
    b = 20;
}

Things to be noticed in the above snippet are.

1. A space between if and “(” .
2. Space in both the sides of the comparison operator.
3. Space between “)” and “{”
4. Space between both the sides of assignment operator (line 2 & 5) . This is true for almost all the operators.
5. Proper indentation of lines 2 & 5. If you are using emacs or vi, check here for your .emacs or .vimrc file .

Well, your code will compile and run even if you don’t give these spaces, but a program coded with a bad coding style is equivalent to an inefficient code.

Sample Code 2

Let us have a function which takes two integers and returns their sum .
The code should be like

int add_numbers (int num1, int num2) {

    return (num1 + num2);
}

The function call will be something like,

int sum;
sum = add_numbers (10, 20);

Things to be noticed in the above snippet is

In the first line in the function declaration,

1. The function name should be as clear as possible.
2. A space between the end of function name and “(” .
3. Spaces are given after every “,” in the function argument list.
4. A space is given between “)” and “{“.

In the second line in the function declaration,

1. A space before “(“. [ This rule is almost global. Apply it everywhere whenever you use “(” ] .

2. There is a space on both the sides of the addition operator. This is again almost global. A space between both the sides of operator makes the code look real neat.

3. The indentation about which was mentioned earlier.

But yes, if your girl friend is a geek or a nerd or a psycho or a fundoo, then you better go for this. 😉

#define MAGIC “eilouvy43605321”
#define _(p,o,q) (t o#p[0])?(q)
#define __(p,o,q) _(p,o,t-q)
int main(){int t, i; for(i=8;i>0;i–)printf(“%c”, MAGIC[(((t=(MAGIC+7)[i-1])==’_’)?62:_(.,==,63):_(@,==,64):__(a,>=,’a’+36):__(A,>=,’A’+10):(t-‘0’))]);}

Note :: I wont say the coding style i use is the perfect one. It always depends upon what your team was using till now and how easy it is to read, debug and maintain the code.

Useful Links :

The guide coding standards in GNOME is really a nice one.
Even better was this one i found recently. Though i didn’t read it completely, it was quite interesting.
This article was short and sweet.

2 Comments

  1. Pingback: int num2

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.