Re: Code consolidation




"Aaron Jackson" <none@xxxxxxxxxx> 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.
> 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.
>
The chi-squared test is inherently a comparision between two counts.
So write the function

double chi_squared(double *observed, double *expected, int N)

Now you data may not come in flat arrays of doubles. Not to worry, simply
write a little bit of "glue" code to transform it.
It is most unlikely that your chi-squared test is time critical. If it is,
then you do need to hard code some aspects of the test, to avoid this
overhead. Software engineering is often about compromises.


.



Relevant Pages

  • Code consolidation
    ... I am in the process of writing my first large scale program and I find ... double deltay, chiSqrd; ...
    (comp.lang.c)
  • Re: Code consolidation
    ... >I am in the process of writing my first large scale program and I find ... > double deltay, chiSqrd; ... type signature (they all receive the same parameter types (even if they ... required netXXXX function as a parameter to a single Chisqrd function. ...
    (comp.lang.c)