Wednesday, July 2, 2008

One Tips :: For Coding Guidelines


One problem is solved by two methods. Can you tell me which is better and why?

Method 1:

void client1()
{
fun();
}
void clenit2()
{
fun();
}
...............
...............
...............
...............
void clientN()
{
fun();
}

void fun()
{
if(con1)
return;
if(con2)
return;
if(con3)
return;
//Rest Code is functionalitywise of fun as
....
....
}

Method 2:

void client1()
{
if(!con1 || !con2 || !con3)
fun();
}
void clenit2()
{
if(!con1 || !con2 || !con3)
fun();
}
...............
...............
...............
...............
void clientN()
{
if(!con1 || !con2 || !con3)
fun();
}

void fun()
{
//functionality of fun as
}

My Point Of View:

Case1: No question if either con1or con2 or con3 will be false , Offcourse Method 1 will be the best. Atleast No duplicasy of code.No Overhead..Fine

Case2: In case Of Either con1 or con2 or con3 =true then

In case of method1 , Every clients will make a stack frame while calling fun() ...Costly ...overhead!!! But No duplicasy of code..Seing code nicely

In case of method2, Every clients will not call fun()....Not Costly..No Overhead!!! But duplicasy of code...Bad looking code

Results:
Now both has advantages and disadvantages, then what you will do? Now we have to check our fun()'s Objective. suppose clients calls fun() 20% of his some life. Then i will suggest to use Method1. Else totally depends on objective of fun().
Give your views. I am waiting for your comments.

1 comment:

Anonymous said...

Hi

I saw ypur many articles in Go4Expert.com,CodeProjects.com,cplusplus.com etc. It's really nice . Here you have given this tips for coding guidelines is very nice.
According to you we have to check code coverage of our product/project for a particular function. so we can know how much uses of it...Am i right else correct me??