> Ruben J=F6nsson schreef op 06-Apr-14 2:24 PM: > > Regarding goto: > > > > Here is a piece of code from the demo app for the TCP/IP stack from Mic= rochip: > > > > ... > > // Read all browser POST data > > while(curHTTP.byteCount) > > { > > // Read a form field name > > if(HTTPReadPostName(curHTTP.data, 6) !=3D HTTP_READ_OK) > > goto ConfigFailure; > > =09 > > // Read a form field value > > if(HTTPReadPostValue(curHTTP.data + 6, sizeof(curHTTP.data)-6-2) != =3D > > HTTP_READ_OK) > > goto ConfigFailure; > > > > //some more test cases here which all jumps to ConfigFailure on fa= ilure > > } > > > > // do something if all is ok here > > return HTTP_IO_DONE > > ConfigFaulure: > > // Do something else on failure here > > return HTTP_IO_DONE > > } > > =09 > > Of course this could be handled without goto but it seems like a good w= ay to > > handle this with a goto here. I guess it all depends on how the goto is= used. > > > > >=20 > IMO this would be better written either >=20 > - with the while and tests in one function, with 'return failure'=20 > instead of the goto's; call this function from another function, which=20 > does the 'postprocessing. >=20 > or >=20 > - with 'status =3D failure; break;' at the goto's, and a single if at the= =20 > end which tests the status. >=20 > Wouter I am not sure that I agree that this would be better written in this case. = The=20 first option makes the code harder to read since I have to go and look up t= he=20 function to know what it is doing. I also have to pass along some variables= =20 and/or pointers and of course calling the function which introduces overhea= d on=20 the compiled code (not by much but still...). I would probably use the second option myself, mostly because I have been=20 taught that goto is an evil statement in C. However, here it introduces one= =20 more variable that has to be initialized, a break that could be forgotten (= and=20 only discovered if all if statements are tested against a failure result). = It=20 also makes the code harder to read since I have to go down and check what=20 happens after the break. break is actually just a goto anyway but with the= =20 difference that it is not always obvious where it goes too or what it does.= A=20 goto with a meaningful label on the other hand... I have now learned that goto is not an evil statement but it can be used in= an=20 evil way. There is actually a reason why it does exist. I am not trying to convince you or anyone else to start using gotos in C bu= t=20 just saying that sometimes it does make sense to do so and it may be the be= st=20 option (such as in this case, in my opinion). /Ruben --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .