Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!ncar!boulder!wolniewi From: wolniewi@boulder.Colorado.EDU (WOLNIEWICZ RICHARD HANSON) Newsgroups: comp.lang.modula2 Subject: Re: Why are the loops so awkward? Message-ID: <22259@boulder.Colorado.EDU> Date: 14 Jun 90 18:09:35 GMT References: <5377.26731960@puddle.fidonet.org> <728.26767558@waikato.ac.nz> <22213@boulder.Colorado.EDU> <747.2677cd57@waikato.ac.nz> Sender: news@boulder.Colorado.EDU Reply-To: wolniewi@tramp.Colorado.EDU (WOLNIEWICZ RICHARD H.) Organization: University of Colorado, boulder Lines: 75 In article <747.2677cd57@waikato.ac.nz> ccc_ldo@waikato.ac.nz (Lawrence D'Oliveiro, Waikato University) writes: >In <22213@boulder.Colorado.EDU>, wolniewi@boulder.Colorado.EDU >(WOLNIEWICZ RICHARD HANSON) points out (in answer to my suggestion >about only using zero-based arrays where it makes sense to) that >Modula-2 _requires_ open arrays to be zero-based. > >Agreed. In which case it may be better in some cases to avoid open >arrays, and resort to a lower-level technique, on the grounds that >this can make the code easier to understand. > >The lower-level technique I'm thinking of looks like this: [Code using pointer to array and array bounds passing] >Yeah, sure it's yucky. But I submit that there may be sometimes when this >is better than the alternative. Not always, just sometimes. I do agree that there are times that this technique would be useful (I frequently want to get around the array renumbering Modula 2 forces on us) but I would hesitate to use this in most situations, since it complicates the procedure interface and might discourage the next programmer on the project from re-using the code. In the specific instance of avoiding a CARDINAL value constraint, a simple IF statement around the loop would seem easier. [D'Oliveiro then disagrees that using LOOP constructs instead of FOR ... makes the code more difficult to understand, and offers the following code as an example.] > LOOP > IF NrCharsLeft = 0 THEN > StringsSame := TRUE; > EXIT > END (*IF*); > INC(SourceIndex); > DEC(NrCharsLeft); > IF String1[SourceIndex] <> String2[SourceIndex] THEN > StringsSame := FALSE; > EXIT > END (*IF*) > END (*LOOP*) > >Do you think you could write it just as simply and clearly using something >other than LOOP? I don't think so. I would probably write stringsSame := TRUE; WHILE stringsSame AND (index <= stringLength) DO stringsSame := (String1[index] = String2[index]); INC(index); END; (* WHILE *) Here I think it is just as clear, if not more so, that the intent of the programmer is to terminate the loop with stringsSame set correctly, and that the loop will terminate when a difference is found or the index exceeds the string length. Of course, this is where the opinionated nature of looping constructs comes in. >Loops a religious issue? Now why would that be? In my days as a Computer >Science student we used to argue about this sort of thing all the time. >So long as the debate stays calm and reasonable, I'm happy to take part in >it! I agree! That's why I have subscribed to this group, after all; to take part in discussions which will hopefully help us do this programming thing better. p.s. Hansen is my middle name, which the University recorded incorrectly. Please feel free to call me 'Richard', 'Wolniewicz' being somewhat cumbersome. . . ______ Laboratory for Atmpospheric and Space Physics | / \ (__ ._) University of Colorado, Boulder |__/ \____) | Richard Wolniewicz (wolniewi@tramp.colorado.edu) Disclaimer: Only the opinions of a grad student (me)