Thanks. My question is more on the general view (I use GCC on Linux, UNIX, but sometimes use ARM ADS on some projects as well).
I always worry about putting "break" in for loop and while loops because I am not sure if it is a bad practice (as using goto). So I only use it in case statements. Should I avoid "break" if possible?
No, it's a good thing, if you know what I mean. But if you have a large set of cases, try to group those that end in 'break' together and those that end in 'return' together, et cetera. - RM
break (and continue) preserve more information about loop states than does a blunt goto. Therefore it is easier to optimize code using them than code using goto.
As far as clarity is concerned, that is another matter IMO. I consider that gotos used only to replace the effects of break and continue would be clearer, because they would contain a clear identification of the place gone to, and that place would in turn be clearly identified with a sited label.
My advice is the same as for gotos - use them when needed, but avoid the need. I think your instincts are sound. Now you may have some reasons to back them up.
--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
USE worldnet address!
Using break is an accepted practice. Stylistically you should take care to call attention to a break with a comment or by setting it off in braces because the next person reading your code could easily overlook it.
Someone once suggested to me that, rather than use "break", it was clearer to set a loop termination flag and use "continue" to force the exit. This isn't totally crazy because in many CPU architectures it is more efficient to code the test at the end of a loop and branch backward to the beginning if necessary. Using "break" is more efficient for exiting a single loop because it is unconditional, but the "continue" technique can easily be used to exit from nested loops whereas the "break" cannot.
I have only rarely used the "continue" method, but I always keep it in mind in case it happens to fit the pattern of the code I am writing.
In many situations where such dilemmas occur, however, a (guess what!) state machine will improve readability and hence maintainability, and make such issues irrelevant.
OTOH, the goto is not constrained to have any meaning with respect to the construct it's used in, where a break or continue has a clearly defined effect with respect to that construct.
--
Al Balmer
Balmer Consulting
removebalmerconsultingthis@att.net
In general, error recovery tends to take a nice clean architecture and make a mess of it. One reason why too many people ignore it (along with the fact that in many cases there isn't a clean recovery method possible).
Any code (error recovery or other) needs to be judged by clarity and readability. It's possible to create very unclear but properly structured code or clear code with gotos.
Of course they would never do that :-^ They have only be worried about creating a good product that works for the last n years of course :-)
Note seen comment recently on computer security in IEEE Computer Society magazine this month, where someone quotes about PC Firewall products that not enough testing and rushing to market abounded.
--
Paul Carpenter | paul@pcserv.demon.co.uk
Main Site
GNU H8 & mailing list info.
For those web sites you hate.
ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here.
All logos and trade names are the property of their respective owners.