c# - How do I get rid of this goto? -


i started position , @ end of workday wait out traffic reading through our codebase. came across bit , after fair amount of time @ whiteboard still can't think of way extract goto. there way excise jump?

public void myupdate(mytype foo) {     /*prep code loops*/             foreach (thing bar in something)     {         foreach (collection item in bar.stuff)         {             data datarx = item.first;             if (datarx != null && datarx.id.equals(globalnonsense.id))             {                 // update data latest changes                 datarx.foobuddy = foo;                 goto exitloops;             }         }     }      exitloops: ; } 

since label exitloops @ end of method, can use return exit method this:

if (datarx != null && datarx.id.equals(globalnonsense.id)) {     // update data latest changes     datarx.foobuddy = foo;     return; } 

another approach use flag this:

bool done = false;  foreach (thing bar in something) {     foreach (collection item in bar.stuff)     {         data datarx = item.first;         if (datarx != null && datarx.id.equals(globalnonsense.id))         {             // update data latest changes             datarx.foobuddy = foo;             done = true;             break;         }     }      if(done)         break; } 

you can use second approach if there code after label.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -