Wednesday, May 23, 2007

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.

Monday, May 21, 2007

Feel My Pain...

One day in heaven, the Lord decided He would visit the earth and take a stroll. Walking down the road, He encountered a man who was crying.

The Lord asked the man, "Why are you crying, my son?" The man said that he was blind and had never seen a sunset. The Lord touched the man who could then see and was happy.

As the Lord walked further, He met another man crying and asked, "Why are you crying, my son?" The man was born a cripple and was never able to walk. The Lord touched him and he could walk and he was happy.

Farther down the road, the Lord met another man who was crying and asked, "Why are you crying, my son?" The man said, "Lord, I am an engineer."

...and the Lord sat down and cried with him.

Sunday, May 20, 2007

PODWORKS.in

India's Biggest Event on Audio & Video Podcasting

Date: June 9 & 10 (Saturday & Sunday)

Venue: Tidel Park Auditorium, Chennai.

Cost: Rs.200

Alagappan designed banners saying I'm Attending Podworks.in and I'm speaking at Podworks.in. You can grab those here.

PodWorks is an ideal place to learn how to start your own podcast or to discover the latest tips and tricks for taking your show to the next level. The presentations offered will cover the whole spectrum from content production, technical how-to, business podcasting, marketing and monetization. Each session is an exchange of ideas.

I am really looking forward for this event, coz apart from the podcasting , it will be a re-union kind , where i will be meeting alagappan :) :) . Infact that's the main reason why i am attending this ;-)

You can register here and make the event a big success :)

Friday, May 18, 2007

Hello World ...

I love Hello World programs.
The reason is simple. Whenever i code a hello world, it means i am learning something new. I once found a huge collection of hello world programs here.

And yes, i did a hello world program today. It is related cluster programming using MPI . :-) .

The code for the hello world

#include <stdio.h>
#include <mpi/mpi.h>
int main(int argc, char *argv[]) {
int err;
err = MPI_Init(&argc, &argv);
printf ("Hello World\n");
err = MPI_Finalize();
}

Since i dont have a cluster at home, i had to simulate a cluster. That can be done using the mpirun command .

hari@home:~/spider/cluster$ mpirun -np 3 a.out
Hello World
Hello World
Hello World
hari@home:~/spider/cluster$

For the first time, this asked for my password 3 times ( the number of process i mentioned ) . After that i created a keygen for myself and then things went fine . But it took a long time when i gave the number of processes as 100 .

Planning to code for a fractal using MPI in my free time :) .

Wednesday, May 09, 2007

Life ...

Life is a beautiful poem with so many pages of lyrics written on every page. It depends on whether you sing a song with it or think it as a greek stupid probability junk and leave it .
I am gonna sing :-) :-)

Thursday, May 03, 2007

Seg Fault

Wikipedia says,
A segmentation fault (often shortened to segfault) is a particular error condition that can occur during the operation of computer software. A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system)."

I have seen hundreds and hundreds of seg faults ;-) while coding for record module of LDTP and Spider SMS. But the one i saw yesterday was new, strange and fascinating. I am not sure whether i will be able to reproduce it again. The screenshot says why it is strange and fascinating :-) .




-bash-3.1$ man su
says

AUTHOR
Written by David MacKenzie.

REPORTING BUGS
Report bugs to <bug-coreutils@gnu.org>.


Maybe i should consider reporting this :P .