Re: Code consolidation
- From: Chris McDonald <chris@xxxxxxxxxxxxxxx>
- Date: Wed, 31 Aug 2005 03:09:32 +0000 (UTC)
Aaron Jackson <none@xxxxxxxxxx> writes:
>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.
>For example:
>double
>Chisqrd1(double *params, double *weight) { // CHANGE
> int i;
> double deltay, chiSqrd;
> xy_t *data;
> chiSqrd = 0.0;
> data = dataList;
> for (i = 0; data != NULL; i++) {
> deltay = data->y - netCarriers(params, data->x); // CHANGE
> chiSqrd += weight[i] * deltay * deltay;
> data = data->next;
> }
> return(chiSqrd);
>}
>double
>Chisqrd2(dopant_t *dopants, double *weight, double *Ef, double Eg) { //
>CHANGE
> int i;
> double deltay, chiSqrd;
> xy_t *data;
> chiSqrd = 0.0;
> data = dataList;
> for (i = 0; data != NULL; i++) {
> deltay = data->y - netIonized(dopants, data->x, Ef[i]); // CHANGE
> chiSqrd += weight[i] * deltay * deltay;
> data = data->next;
> }
> return(chiSqrd);
>}
>I was wondering if there are common ways to cut down on this type of
>duplication without adding too much to the overall complexity. Does
>anybody have any suggestions? Thanks.
I suggest making all of your functions, netXXXX, to be of the same
type signature (they all receive the same parameter types (even if they
don't use them all), and all return the same type), and then passing the
required netXXXX function as a parameter to a single Chisqrd function.
--
Chris.
.
- References:
- Code consolidation
- From: Aaron Jackson
- Code consolidation
- Prev by Date: Re: C FAQ wiki
- Next by Date: Re: Why C/C++ errors are SO obscure/devious??
- Previous by thread: Code consolidation
- Next by thread: Re: Code consolidation
- Index(es):
Relevant Pages
|