473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Code consolidation

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.

Aaron
Nov 15 '05 #1
8 1352
Aaron Jackson <no**@nospam.com> 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.
Nov 15 '05 #2
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!

Nov 15 '05 #3
Consider putting all the similay functions in their own files.
Then they're all in front of you and then decisions perhaps will become
clearer.

george

Nov 15 '05 #4

"Aaron Jackson" <no**@nospam.com> 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.
Nov 15 '05 #5
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.
For example: expand chisqrd.c

#include <stdlib.h>

typedef struct dopant_t dopant_t;
typedef struct xy_t xy_t;
struct xy_t {
double x;
double y;
xy_t* next;
};

extern
xy_t* dataList;

double
netCarriers(const double[], double);

double
Chisqrd1(const double params[], const double weight[]) {
// CHANGE
xy_t* data = dataList;
double chiSqrd = 0.0;
for (size_t i = 0; NULL != data; ++i) {
double deltay = data->y - netCarriers(params, data->x);
// CHANGE
chiSqrd += weight[i]*deltay*deltay;
data = data->next;
}
return chiSqrd;
}

double
netIonized(const dopant_t*, double, double);

double
Chisqrd2(const dopant_t* dopants, const double weight[],
const double Ef[], const double Eg[]) {
// CHANGE

xy_t* data = dataList;
double chiSqrd = 0.0;
for (size_t i = 0; NULL != data; ++i) {
double
deltay = data->y - netIonized(dopants, data->x, Ef[i]);
// CHANGE
chiSqrd += weight[i]*deltay*deltay;
data = data->next;
}
return chiSqrd;
}
Nov 15 '05 #6
E. Robert Tisdale wrote:
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. For example:


> expand chisqrd.c

This is unbelievable. We try hard to make people understand that in
posting to newsgroups they should use spaces rather than tabs, and
Trollsdale purposely turns spaces into tabs.
Nov 15 '05 #7
jxh

Martin Ambuhl wrote:
E. Robert Tisdale wrote:
> expand chisqrd.c

This is unbelievable. We try hard to make people understand that in
posting to newsgroups they should use spaces rather than tabs, and
Trollsdale purposely turns spaces into tabs.


Well, expand actually turns tabs into spaces.

-- James

Nov 15 '05 #8
jxh wrote:
Well, expand actually turns tabs into spaces.


unexpand turns spaces back into tabs.
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
4
by: jason | last post by:
Hello. Newbie on SQL and suffering through this. I have two tables created as such: drop table table1; go drop table table2; go
16
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
109
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
9
by: Kev | last post by:
I've been trying to understand the article "Identifying Memory Leaks in the Common Language Runtime" listed on http://support.microsoft.com/?id=318263 It says "The garbage collector can collect...
6
by: www.gerardvignes.com | last post by:
Hi, Have you run into a situation where you had to switch from PHP 4.x.x to Perl 5.x.x in order to get better performance? I am using an OO approach to PHP for my website's server code. There...
10
by: bobtehdog | last post by:
Column Consolidation. My problem is this. I have to sort each of the def1-def23 columns from the old table into the def1-def6 columns from the new table preserving the other data in the records....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.