Re: Code consolidation



Aaron Jackson wrote:
> I am in the process of writing my first large scale program and I find
> myself in the position of writing several copies of functions that do
> the same thing, with mostly the same code, but only slightly different.
(...)
try macros:

#define CHI(funDeclare,specifics)\
double funDeclare{\
int i;\
double deltay,chiSqrd;\
xy_t *data;\
\
chiSqrd = 0.0;\
data = dataList;\
for (i = 0; data != NULL; i++) {\
deltay = data->y - specifics;\
chiSqrd += weight[i] * deltay * deltay;
data = data->next;\
}\
return(chiSqrd);\
}

CHI(
Chisqrd1(double *params, double *weight),
netCarriers(params, data->x)
)

CHI(
Chisqrd2(dopant_t *dopants, double *weight, double *Ef, double Eg),
netIonized(dopants,data->x, Ef[i])
)

I don't like it at all, bit it is a valid solution

Have you read about templates in C++? They could have been a nicer solution!

.