optimization - Does replacing repititve "if" block with a macro reduce code complexity in C++? -


i had question. need refactor part of code reduce complexity. have multiple similar "if" constructs repeats after every random utility api call, based on need rollback transaction done or else continue. wondering whether replacing "if" construct me reducing complexity. know macro expands in code , why not sure whether approach me. new @ refactoring codes. if idea , knowledge on refactoring suggest me way, helpful. replacing below code

retcode = certman_set_cmp_server_psk_refnum(ctx,(char*)domain.c_str(),null,null,0); if (retcode != 0) {     if(startedtxn == true)     {         status = ldapinterface.rollback_transaction();         if(0 != status)         {             clifwk_trace(session, "rollback failed in internal transaction", clifwk_debug);             syslog(log_err, "rollback failed in internal transaction");             return certman_commit_transaction_failed;         }         del_if_not_null (ctx);     }     return retcode; } 

with retcode = certman_set_cmp_server_ip_port(ctx,(char*)domain.c_str(),null,0); check_rollback_transaction(session,ctx,ldapinterface,retcode,startedtxn); return_if_error(retcode);

will approach work me reduce complexity ?

your complexity problem appears caused c, little c++. adding more c in form of macro's makes worse.

for instance, del_if_not_null (ctx); not 1 2 three flags. assume it's if (ctx) delete ctx construct. first red flag if statement, doesn't anything. second red flag you're using raw pointers, instead of relying on smart pointers. have fixed third red flag, , that's failure clean in 0!=status error case.

similar complexicy caused using retcode instead of exceptions.

this how simple code should be:

std::unique_ptr<t> ctx = foo(); auto transaction = ldapinterface.get_transaction(); // rollback unless committed certman::set_cmp_server_psk_refnum(*ctx,domain,nullptr,nullptr,0); transaction.commit(); 

Comments