> -----Original Message----- > From: William Chops Westfield [SMTP:billw@CISCO.COM] > Sent: Wednesday, April 11, 2001 11:14 AM > To: PICLIST@MITVMA.MIT.EDU > Subject: Re: [OT]: Coding style > > Style opinion: put spaces after your "if"s. > > It's not clear to me that avoiding multiple returns in a situation like > this > is really that important, but if you want to avoid it, I might do somthing > like this : > > success = false; > while (1 /* NOT ERROR */) { > if (!I2CStart()) > break; > > if (!I2CTransmit(fullAddr)) > break; > > if (!I2CGetAck()) > break; > > if (!I2CTransmit(instruction)) > break; > > if (!I2CGetAck()) > break; > > if (!I2CStop()) > break; > > success = true; > break; > } > return success; > > A DO loop would theoretically be more appropriate, but they're uncommon > and > occasionally frowned upon stylistically. Someone mentioned "break" > shouldn't be used in while loops? I haven't heard that one... (I > stripped > your comments, but you shouldn't!) You could make it "while (!success)", > but that'll generate rather pointless code. As written, there's no extra > jump to the beginning of the loop, and a good compiler will probably > remove > the loop-related instructions entirely. > > This *is* the sort of situation where one can sometimes get away with > GOTO. > Neither your original code nor the above "improvement" makes it obvious > that > the failure returns are error conditions as clearly as something like: > > if(!I2CStart()) > goto write_error; > > if(!I2CTransmit(fullAddr)) > goto write_error; > : > > BillW > The initial version of this driver used goto's to a common exit point which I considered a reasonable use of a goto. However, a colleague made some disparaging comments about it and made me think twice about it! I think that Code Complete mentions that break's within loops can make for difficult reading, but also says that, as with almost anything, there are exceptions to the rule. The above seems reasonable. Thanks for the suggestions. Mike -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads