yzheng@thoughtworks.com



char* servNumber;

XString domain;
db->getParameter(”domain", domain);

if (0 == strcmp(domain.getData(), "")) {
    domain.setData(servNumber);
} else {
    XString tmpValue;
    XString tmpServnumber;
    tmpServnumber.setData(servNumber);
    tmpValue = tmpServnumber + domain;
    domain.setData(tmpValue.getData());
}

XString& servNumber;
XString domain;
db->getParameter(”domain", domain);

domain = servNumber + domain;




if (1 == insertFlag) {                if (1 == insertFlag) {
    retList.insert(i, newCatalog);        retList.insert(m, newCatalog);
} else {                              } else {
    retList.add(newCatalog);              retList.add(newCatalog);
}!                                    }     !


if (1 == insertFlag) {
    retList.insert(j, newPrivNode);
} else {
    retList.add(newPrivNode);
}!



if ( "" != *OPRNUMB) {
    para.insert("OPRNUMB", OPRNUMB->getData());
}

if ( "" != *MSISDN) {
    para.insert("TELNUM", MSISDN->getData());
}

if ( "" != *TMSISDN) {
    para.insert("TMSISDN", TMSISDN->getData());
}!



if ( strcmp(type, “DropGroup") == 0
    || strcmp(type, "CancelUserGroup") == 0
    || strcmp(type, "QFUserGroup") == 0
    || strcmp(type, "CancelQFUserGroup") == 0
    || strcmp(type, "QZUserGroup") == 0
    || strcmp(type, "CancelQZUserGroup") == 0
    || strcmp(type, "SQUserGroup") == 0
    || strcmp(type, "CancelSQUserGroup") == 0
    || strcmp(type, “UseGroup") == 0
    || strcmp(type, "CancelGroup") == 0)


                                            	


if             	

if (0 == retCode) {
  SendMsg("000", "Process Success", outResult);
} else {
  SendMsg("000", "Process Failure", outResult);
}




const char* msg = (0 == retCode
                         ? "Process Success"
                         : "Process Failure");
SendMsg("000", msg, outResult);
if(!strcmp(pRec->GetType(), RECTYPE::INSTALL)) {
  CommDM.ChangGroupInfo(
    const_cast(CommDM.GetAttr("IPAddress",
      &(pGroup->m_Attr))),
    true);
} else {
  CommDM.ChangGroupInfo(
    const_cast(CommDM.GetAttr("IPAddress",
      &(pGroup->m_Attr))),
    false);
}

switch(firstChar) {
case ‘N’:
  nextFirstChar = ‘O’;
  break;
case ‘O’:
                                      switch(firstChar) {
  nextFirstChar = ‘P’;
                                      case ‘N’:
  break;
                                      case ‘O’:
case ‘P’:
                                      case ‘P’:
  nextFirstChar = ‘Q’;
                                      case ‘Q’:
  break;
                                      case ‘R’:
case ‘Q’:
                                        nextFirstChar = firstChar + 1;
  nextFirstChar = ‘R’;
                                        break;
  break;
                                      case ‘T’:
case ‘R’:
                                        throw IllegalArgumentException();
  nextFirstChar = ‘S’;
                                      default:
  break;
                                      }!
case ‘S’:
  nextFirstChar = ‘T’;
  break;
case ‘T’:
  throw IllegalArgumentException();
default:
}



if (firstChar >= ‘N’ && firstChar <= ‘S”) {
    nextFirstChar = firstChar + 1;
} else {
    throw IllegalArgumentException();
}!


void processService(const char *oprCode, const char *subID,
                      const char *oID, XList *callCicsList) {
   Xstring relaPri(“NULL”);	

   Xstring relaSec(“NULL”);	

   Xstring scoutBySec(“0”);	

   XList *tempList = new Xlist;
   callCicsList->add(tempList);
   tempList->add(new XString(oprCode));	

   tempList->add(new XString(oID));	

  XString *psTelNum = new XString;
   tempList->add(psTelNum);	

  GetServnumberBySubsID(subID, *psTelNum);
   tempList->add(new CEntityString(esRelaPri.GetData()));	

   tempList->add(new CEntityString(esRelaSec.GetData()));	

  tempList->add(new CEntityString(esScoutBySec.GetData()));	

}!



ColdRule *newRule = new ColdRule();
newRule->SetOID(oldRule->GetOID());
newRule->SetRegion(oldRule->GetRegion());
newRule->SetRebateRuleID(oldRule->GetRebateRuleID());
newRule->SetBeginCycle(oldRule->GetBeginCycle() + 1);
newRule->SetEndCycle(oldRule->GetEndCycle());
newRule->SetMainAcctAmount(oldRule->GetMainAcctAmount());
newRule->SetGiftAcctAmount(oldRule->GetGiftAcctAmount());
newRule->SetValidDays(0);
newRule->SetGiftAcct(oldRule->GetGiftAcct());
rules->Add(newRule);


ColdRule* CreateNewRule(ColdRule& oldRule) {
  ColdRule *newRule = new ColdRule();
  newRule->SetOID(oldRule.GetOID());
  newRule->SetRegion(oldRule.GetRegion());
  newRule->SetRebateRuleID(oldRule.GetRebateRuleID());
  newRule->SetBeginCycle(oldRule.GetBeginCycle() + 1);
  newRule->SetEndCycle(oldRule.GetEndCycle());
  newRule->SetMainAcctAmount(oldRule.GetMainAcctAmount());
  newRule->SetGiftAcctAmount(oldRule.GetGiftAcctAmount());
  newRule->SetValidDays(0);
  newRule->SetGiftAcct(oldRule.GetGiftAcct());
  return newRule
}

rules->Add(CreateNewRule(*oldRule));

return bool

int RecComm::setIDBySevNum(const CEntityString& servnumber ) {
   DB db;
   db.setSQL("select id from users where servnumber=:servnumber");
   db.bind(":servnumber", servnumber.c_str());
   db.open();

  if (!db.next()) {
      return -1;
  }

  setID(db.getString(”id"));
  return 0;
}!
reference         	


void Opcode::notifyCRM(const char * prodid,
                          const char * paramid,
                          CEntityString * retparam) {
  if (NULL == prodid || NULL == paramid
   || NULL == retparam) {
      throw IllegalArgumentsException (
         Error::GetErrorMsg(”880022", "Opcode::notifyCRM"));
  }

  …
}!




	





                  	

      	




•                              	

•                        	

•                 	

•  if/while/for                      	

•                                                 	

•                                           	

•                  	



•    	

•           Effective XX   	

http://dreamhead.blogbus.com/tag/   /

http://www.infoq.com/cn/ugly-code




Ugly code

  • 1.
      yzheng@thoughtworks.com
  • 2.
  • 3.
  • 4.
  • 5.
    char* servNumber; XString domain; db->getParameter(”domain",domain); if (0 == strcmp(domain.getData(), "")) { domain.setData(servNumber); } else { XString tmpValue; XString tmpServnumber; tmpServnumber.setData(servNumber); tmpValue = tmpServnumber + domain; domain.setData(tmpValue.getData()); }
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
     if (1 ==insertFlag) { if (1 == insertFlag) { retList.insert(i, newCatalog); retList.insert(m, newCatalog); } else { } else { retList.add(newCatalog); retList.add(newCatalog); }! } ! if (1 == insertFlag) { retList.insert(j, newPrivNode); } else { retList.add(newPrivNode); }!
  • 11.
     if ( ""!= *OPRNUMB) { para.insert("OPRNUMB", OPRNUMB->getData()); } if ( "" != *MSISDN) { para.insert("TELNUM", MSISDN->getData()); } if ( "" != *TMSISDN) { para.insert("TMSISDN", TMSISDN->getData()); }!
  • 12.
  • 13.
     if ( strcmp(type,“DropGroup") == 0 || strcmp(type, "CancelUserGroup") == 0 || strcmp(type, "QFUserGroup") == 0 || strcmp(type, "CancelQFUserGroup") == 0 || strcmp(type, "QZUserGroup") == 0 || strcmp(type, "CancelQZUserGroup") == 0 || strcmp(type, "SQUserGroup") == 0 || strcmp(type, "CancelSQUserGroup") == 0 || strcmp(type, “UseGroup") == 0 || strcmp(type, "CancelGroup") == 0) 
  • 14.
  • 15.
    if  if (0 == retCode) { SendMsg("000", "Process Success", outResult); } else { SendMsg("000", "Process Failure", outResult); } const char* msg = (0 == retCode ? "Process Success" : "Process Failure"); SendMsg("000", msg, outResult);
  • 16.
    if(!strcmp(pRec->GetType(), RECTYPE::INSTALL)) { CommDM.ChangGroupInfo( const_cast(CommDM.GetAttr("IPAddress", &(pGroup->m_Attr))), true); } else { CommDM.ChangGroupInfo( const_cast(CommDM.GetAttr("IPAddress", &(pGroup->m_Attr))), false); }
  • 17.
  • 18.
    switch(firstChar) { case ‘N’: nextFirstChar = ‘O’; break; case ‘O’: switch(firstChar) { nextFirstChar = ‘P’; case ‘N’: break; case ‘O’: case ‘P’: case ‘P’: nextFirstChar = ‘Q’; case ‘Q’: break; case ‘R’: case ‘Q’: nextFirstChar = firstChar + 1; nextFirstChar = ‘R’; break; break; case ‘T’: case ‘R’: throw IllegalArgumentException(); nextFirstChar = ‘S’; default: break; }! case ‘S’: nextFirstChar = ‘T’; break; case ‘T’: throw IllegalArgumentException(); default: }
  • 19.
     if (firstChar >=‘N’ && firstChar <= ‘S”) { nextFirstChar = firstChar + 1; } else { throw IllegalArgumentException(); }!
  • 20.
  • 21.
     void processService(const char*oprCode, const char *subID, const char *oID, XList *callCicsList) { Xstring relaPri(“NULL”);  Xstring relaSec(“NULL”);  Xstring scoutBySec(“0”);  XList *tempList = new Xlist; callCicsList->add(tempList); tempList->add(new XString(oprCode));  tempList->add(new XString(oID));  XString *psTelNum = new XString; tempList->add(psTelNum);  GetServnumberBySubsID(subID, *psTelNum); tempList->add(new CEntityString(esRelaPri.GetData()));  tempList->add(new CEntityString(esRelaSec.GetData()));  tempList->add(new CEntityString(esScoutBySec.GetData()));  }!
  • 22.
  • 23.
     ColdRule *newRule =new ColdRule(); newRule->SetOID(oldRule->GetOID()); newRule->SetRegion(oldRule->GetRegion()); newRule->SetRebateRuleID(oldRule->GetRebateRuleID()); newRule->SetBeginCycle(oldRule->GetBeginCycle() + 1); newRule->SetEndCycle(oldRule->GetEndCycle()); newRule->SetMainAcctAmount(oldRule->GetMainAcctAmount()); newRule->SetGiftAcctAmount(oldRule->GetGiftAcctAmount()); newRule->SetValidDays(0); newRule->SetGiftAcct(oldRule->GetGiftAcct()); rules->Add(newRule);
  • 24.
     ColdRule* CreateNewRule(ColdRule& oldRule){ ColdRule *newRule = new ColdRule(); newRule->SetOID(oldRule.GetOID()); newRule->SetRegion(oldRule.GetRegion()); newRule->SetRebateRuleID(oldRule.GetRebateRuleID()); newRule->SetBeginCycle(oldRule.GetBeginCycle() + 1); newRule->SetEndCycle(oldRule.GetEndCycle()); newRule->SetMainAcctAmount(oldRule.GetMainAcctAmount()); newRule->SetGiftAcctAmount(oldRule.GetGiftAcctAmount()); newRule->SetValidDays(0); newRule->SetGiftAcct(oldRule.GetGiftAcct()); return newRule } rules->Add(CreateNewRule(*oldRule));
  • 25.
  • 26.
    return bool int RecComm::setIDBySevNum(constCEntityString& servnumber ) { DB db; db.setSQL("select id from users where servnumber=:servnumber"); db.bind(":servnumber", servnumber.c_str()); db.open(); if (!db.next()) { return -1; } setID(db.getString(”id")); return 0; }!
  • 27.
    reference  void Opcode::notifyCRM(const char * prodid, const char * paramid, CEntityString * retparam) { if (NULL == prodid || NULL == paramid || NULL == retparam) { throw IllegalArgumentsException ( Error::GetErrorMsg(”880022", "Opcode::notifyCRM")); } … }!
  • 28.
       
  • 29.
  • 30.
     •   •   •   •  if/while/for  •   •   •  
  • 31.
     •   •  Effective XX 
  • 32.
    http://dreamhead.blogbus.com/tag/ / http://www.infoq.com/cn/ugly-code
  • 33.
  • 34.
  • 35.