c++ - How to factorize code properly? -



c++ - How to factorize code properly? -

i've basic programming question. imagine i've 2 functions definition practically same except vary in internal binary condition. rest of code in both functions practically same.

in order have readable , easy-to-maintain code, wondering whether there's improve solution other using bool parameter selects operation mode of functions? there design-pattern that?

in code below, i'm illustrating uncertainty 2 functions named doa() , dob(). dots (....) correspond code same both functions. created new donew() function additional boolean parameter chose appropriate functionality. however, note although possible solution, it's still inefficient because of duplicated code within body of both if conditions.

void doa( ..... ){ ..... ..... if(x!=y){ .... .... .... } ..... } void dob( ..... ){ ..... ..... if(x==y){ .... .... .... } ..... } void donew( ....., bool selectionmode ){ ..... ..... if(selectionmode == true){ if(x==y){ .... .... .... } } else{ if(x!=y){ .... .... .... } } ..... }

i utilize boolean parameter distinguisch between them. not utilize complicated pattern simple procedure.

i use

void doit(..., bool is_equal) { ... if((a == b) == is_equal) { // or: is_equal ^ (a == b) ... } ... }

to ultimatively cut down redundance. define independent names

void doa(...) { doit(..., true); } void dob(...) { doit(..., false); }

because consider flag parameters in api bad.

c++ function methods

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -