From: James Edward Gray II Date: 2005-12-14T10:36:50+09:00 Subject: Re: Idiom wanted: do-while On Dec 13, 2005, at 7:20 PM, Steve Litt wrote: > On Tuesday 13 December 2005 11:09 am, Jacob Fugal wrote: >> On 12/12/05, Steve Litt wrote: >>> On Monday 12 December 2005 05:42 pm, James Edward Gray II wrote: >>>> loop do >>>> # ... some action ... >>>> break unless ... >>>> end >>> >>> I do this quite a bit, but it's not structured programming and is a >>> little like a goto. >> >> I see no similarity. 'goto' is unstructured because the target point >> is completely arbitrary; place a label in your code then jump >> right to >> it. 'break' is completely structured; it's part of the structure of >> the enclosing loop and its target is defined by that structure. > > Ahh -- I found a reference. See > http://en.wikipedia.org/wiki/Structured_programming, and note > Dijkstra's > structured programming definition -- every block of code has one > entry point > and one exit point. The loop do ... end construct is infinite. You must add a break to end it. With one break, it has exactly one entry and exit point. >>> Break can improve readability on small loops, but on large loops >>> maintained by multiple people it can become a nightmare. >> >> I agree with James; if the loop is long enough or complex enough for >> these to be a problem, the body of the loop probably needs some >> serious refactoring. > > It absolutely does. Trouble is, in many shops loops start out 8 > lines of code, > and over many, many years, maintenance programmers, many not > experienced, > most not being privy to original design considerations, add > features demanded > by management on ultra-tight schedules. A few years later it's 100 > lines of > code and the break statement is in the middle of it. You may be describing where you work, but you are certainly not describing where I do. ;) I've got two words for you: Unit tests. They were invented to address every single issue you just listed (plus some!) and I assure you, they work. You really, really should give them a try. It will be the best gift you ever give yourself, I promise. James Edward Gray II