Computing.Net > Forums > Programming > Remove Redundancy

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Remove Redundancy

Reply to Message Icon

Name: chaturvedi
Date: November 10, 2008 at 07:45:30 Pacific
OS: Windows XP
CPU/Ram: P4 and 256
Product: Microsoft
Comment:

How to remove redundancy of code?



Sponsored Link
Ads by Google

Response Number 1
Name: chao
Date: November 10, 2008 at 21:40:08 Pacific
Reply:

The simple answer is "use functions". Any code you use more than once can usually be turned into a function that you can call as many times as you need it. (Just watch out for globals -- they can often come back to haunt you when you're moving code around.)

If you want a more involved answer, i'll need a more specific question.


0

Response Number 2
Name: chaturvedi
Date: November 11, 2008 at 09:05:16 Pacific
Reply:

ok the following is the code that i would like to get the redundancy removed from...


//////CODE//////////////////

void Object_Test::cutcopypasteObject()
{
QFETCH(QString,To_Execute);
QFETCH(QString,objectType);
QFETCH(QString,object_to_cut);
QFETCH(QString,object_to_copy);
QFETCH(QString,pasteAt_objectType);
QFETCH(QString,pasteAt);
QFETCH(int,no_of_time_to_paste);

if(To_Execute =="TRUE")
{
int valid_object=0;
int cutflag=0;
int copyflag=0;

TDS_LibManager libMngr ;
TDS_Project prObj ;
TDS_NormalObject nmObj ;
TDS_Defs defsObj; // 2.0.1.0 update

libMngr.openProject(projpath);

prObj.copyFrom ( libMngr.getRootObject () );
nmObj = prObj.getNormalObject ();
defsObj=prObj.getDefs(); // 2.0.1.0 update

if(objectType=="ODBS" || objectType=="PROCESS" || objectType=="EVENT" || objectType=="CONDITIONDATA" || objectType=="ENVGROUP" || objectType=="SUBSYSTEM" || objectType=="LOCATION" || objectType=="ENVVARIABLE") //2.0.1.0 update
{

valid_object=1;
}

if(valid_object==0)
{
qDebug("%s",qPrintable(QString("invalid object Type")));
return;
}

if(valid_object==1)
{
if(object_to_cut!="")
{
cutflag=1;
}

if(object_to_copy!="")
{
copyflag=1;

}

if(cutflag==1 && copyflag==1) // if both are not blank then to perform only copy
{
cutflag=0;
}

if(object_to_cut=="" && object_to_copy=="")
{
qDebug("no object specified for cut or copy operation");

return;
}

////////// copy operation ////////////
if(copyflag==1)
{


if(objectType=="ODBS")
{
int no_of_comma=0;
int no_of_time_to_copy=0;
int verify_multiple_copy=0;
QList<QString> list;
int single_copy=0;
int single_odbs_found=0;
int object_copied=0;
int object_cut=0;
int pasteoperation_done=0;
TDS_ODBS odbsObj;
TDS_Process proObj;
TDS_Event eveObj;
TDS_CondData cObj;


QList<TDS_ODBS> odbsList;


if(object_to_copy.contains(","))
{
no_of_comma= object_to_copy.count(",");
no_of_time_to_copy=no_of_comma+1;
}

if(no_of_time_to_copy>0)
{
list=object_to_copy.split(",");
}
else
single_copy=1;


nmObj.getODBSList(odbsList);


if(no_of_time_to_copy>0)
{
for(int i=0;i<odbsList.count();i++)
{
for(int j=0;j<list.count();j++)
{
if(odbsList[i].getAttributeValue(odbsList[i].A_NAME)==list[j])
verify_multiple_copy++;
}
}
}
/// logic for copying last envgroup
if(no_of_time_to_copy>0 && verify_multiple_copy==no_of_time_to_copy && single_copy==0)
{


for(int j=0;j<list.count();j++)
{
for(int i=0;i<odbsList.count();i++)
{
if(odbsList[i].getAttributeValue(odbsList[i].A_NAME)==list[j])
{
odbsObj=odbsList[i];
libMngr.setCurrentObject(libMngr.getObjectIndex(odbsObj));
}
}
}

}
else if(single_copy==0 && verify_multiple_copy!=no_of_time_to_copy)
{
qDebug("verify=%d",verify_multiple_copy);
qDebug("%s","multiple copy operation failed,please give valid input");
return;
}


/////////////////////////////////

if(single_copy==1)
{
for(int i=0;i<odbsList.count();i++)
{
if(odbsList[i].getAttributeValue(odbsList[i].A_NAME)==object_to_copy)
{
odbsObj=odbsList[i];
libMngr.setCurrentObject(libMngr.getObjectIndex(odbsObj));
single_odbs_found=1;
}
}
}
if(single_odbs_found==0 && single_copy==1)
{
qDebug("%s",qPrintable(QString("odbs:"+object_to_copy+" to be copied not found")));
return;
}

if(single_odbs_found==1 || verify_multiple_copy==no_of_time_to_copy)
{
libMngr.copyObject();
object_copied=1;
}

if(object_copied==1)
{
pasteODBS(libMngr,odbsObj,prObj,pasteAt_objectType,pasteAt,cutflag,copyflag,no_of_time_to_paste);

}

else
{
qDebug("%s","no ODBS copied so paste operation cannot be performed");
}




}/// end of odbs copy


if(objectType=="ENVGROUP")
{
int no_of_comma=0;
int no_of_time_to_copy=0;
int verify_multiple_copy=0;
QList<QString> list;
int single_copy=0;
int single_envg_found=0;
int object_copied=0;
int object_cut=0;
int pasteoperation_done=0;


QList<TDS_EnvGroup> envgroupList;

TDS_EnvGroup envgObj;


if(object_to_copy.contains(","))
{
no_of_comma= object_to_copy.count(",");
no_of_time_to_copy=no_of_comma+1;
}

if(no_of_time_to_copy>0)
{
list=object_to_copy.split(",");
}
else
single_copy=1;


nmObj.getEnvGroupList(envgroupList);


if(no_of_time_to_copy>0)
{
for(int i=0;i<envgroupList.count();i++)
{
for(int j=0;j<list.count();j++)
{
if(envgroupList[i].getAttributeValue(envgroupList[i].A_NAME)==list[j])
verify_multiple_copy++;
}
}
}
/// logic for copying last odbs
if(no_of_time_to_copy>0 && verify_multiple_copy==no_of_time_to_copy && single_copy==0)
{


for(int j=0;j<list.count();j++)
{
for(int i=0;i<envgroupList.count();i++)
{
if(envgroupList[i].getAttributeValue(envgroupList[i].A_NAME)==list[j])
{
envgObj=envgroupList[i];
libMngr.setCurrentObject(libMngr.getObjectIndex(envgObj));
}
}
}

}
else if(single_copy==0 && verify_multiple_copy!=no_of_time_to_copy)
{
qDebug("verify=%d",verify_multiple_copy);
qDebug("%s","multiple copy operation failed,please give valid input");
return;
}


/////////////////////////////////

if(single_copy==1)
{
for(int i=0;i<envgroupList.count();i++)
{
if(envgroupList[i].getAttributeValue(envgroupList[i].A_NAME)==object_to_copy)
{
envgObj=envgroupList[i];
libMngr.setCurrentObject(libMngr.getObjectIndex(envgObj));
single_envg_found=1;
}
}
}
if(single_envg_found==0 && single_copy==1)
{
qDebug("%s",qPrintable(QString("EnvGroup:"+object_to_copy+" to be copied not found")));
return;
}

if(single_envg_found==1 || verify_multiple_copy==no_of_time_to_copy)
{
libMngr.copyObject();
object_copied=1;
}

if(object_copied==1)
{
pasteEnvGroup(libMngr,envgObj,prObj,pasteAt_objectType,pasteAt,cutflag,copyflag,no_of_time_to_paste);

}

else
{
qDebug("%s","no ENVGROUP copied so paste operation cannot be performed");
}




}/// end of envgroup copy

if(objectType=="SUBSYSTEM")
{
int no_of_comma=0;
int no_of_time_to_copy=0;
int verify_multiple_copy=0;
QList<QString> list;
int single_copy=0;
int single_sub_found=0;
int object_copied=0;
int object_cut=0;
int pasteoperation_done=0;


QList<TDS_Subsystem> subList;

TDS_Subsystem subObj;


if(object_to_copy.contains(","))
{
no_of_comma= object_to_copy.count(",");
no_of_time_to_copy=no_of_comma+1;
}

if(no_of_time_to_copy>0)
{
list=object_to_copy.split(",");
}
else
single_copy=1;


nmObj.getSubsystemList(subList);


if(no_of_time_to_copy>0)
{
for(int i=0;i<subList.count();i++)
{
for(int j=0;j<list.count();j++)
{
if(subList[i].getAttributeValue(subList[i].A_NAME)==list[j])
verify_multiple_copy++;
}
}
}
/// logic for copying last odbs
if(no_of_time_to_copy>0 && verify_multiple_copy==no_of_time_to_copy && single_copy==0)
{


for(int j=0;j<list.count();j++)
{
for(int i=0;i<subList.count();i++)
{
if(subList[i].getAttributeValue(subList[i].A_NAME)==list[j])
{
subObj=subList[i];
libMngr.setCurrentObject(libMngr.getObjectIndex(subObj));
}
}
}

}
else if(single_copy==0 && verify_multiple_copy!=no_of_time_to_copy)
{
qDebug("verify=%d",verify_multiple_copy);
qDebug("%s","multiple copy operation failed,please give valid input");
return;
}


/////////////////////////////////

if(single_copy==1)
{
for(int i=0;i<subList.count();i++)
{
if(subList[i].getAttributeValue(subList[i].A_NAME)==object_to_copy)
{
subObj=subList[i];
libMngr.setCurrentObject(libMngr.getObjectIndex(subObj));
single_sub_found=1;
}
}
}
if(single_sub_found==0 && single_copy==1)
{
qDebug("%s","Subsystem:"+object_to_copy+" to be copied not found");
return;
}

if(single_sub_found==1 || verify_multiple_copy==no_of_time_to_copy)
{
libMngr.copyObject();
object_copied=1;
}

if(object_copied==1)
{
pasteSubsystem(libMngr,subObj,prObj,pasteAt_objectType,pasteAt,cutflag,copyflag,no_of_time_to_paste);

}

else
{
qDebug("%s","no SUBSYSTEM copied so paste operation cannot be performed");
}




}// end of subsystem copy


0

Response Number 3
Name: chao
Date: November 13, 2008 at 08:38:35 Pacific
Reply:

It looks like you're doing the same thing for most of the types; just the particular types of items you're getting and working with changes. If your objects can be changed so that they have a common base class/interface, you may be able to have one chunk of code that will work with them all.

Or, you could create a template function that contains the code that's currently repeated within the if's. Pass in the list of stuff as an argument to the function, and you've all but eliminated the differences between the code for each part.

As a side note, all those ints that you're using as flags, would probably be better off as bools. The variables you're checking for whether they're 1 or 0, and that'll never have any other value, are what the bool type was made for. (also, you can say "single_copy=true;" and "if (single_copy) { }", reducing unnecessary comparisons with magic numbers...)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Remove Redundancy

Script to remove lines from a file www.computing.net/answers/programming/script-to-remove-lines-from-a-file/7090.html

Batch that will remove all text und www.computing.net/answers/programming/batch-that-will-remove-all-text-und/15564.html

DOS Batch: Remove Trailing Space www.computing.net/answers/programming/dos-batch-remove-trailing-space/9270.html