From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ad988eb0a9545c86 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-04-18 08:48:21 PST Path: supernews.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newspeer1.nac.net!netnews.com!xfer02.netnews.com!iad-peer.news.verio.net!news.verio.net!sea-read.news.verio.net.POSTED!not-for-mail Newsgroups: comp.lang.ada From: Brian Rogoff Subject: Re: Problem trying to implement generics. In-Reply-To: <9bi7vb$9j7$1@taliesin.netcom.net.uk> Message-ID: References: <9b6jtu$4is$2@taliesin.netcom.net.uk> <9b6m27$68e$1@taliesin.netcom.net.uk> <0JBB6.10484$FD1.1197250@news6-win.server.ntlworld.com> <9b7tce$laf$2@taliesin.netcom.net.uk> <3ADC4320.7ACA3DEC@averstar.com> <9bhoup$h9k$1@taliesin.netcom.net.uk> <3ADC7A79.8E853905@mindspring.com> <9bi7vb$9j7$1@taliesin.netcom.net.uk> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Date: Wed, 18 Apr 2001 15:48:20 GMT NNTP-Posting-Host: 206.184.139.136 X-Complaints-To: abuse@verio.net X-Trace: sea-read.news.verio.net 987608900 206.184.139.136 (Wed, 18 Apr 2001 15:48:20 GMT) NNTP-Posting-Date: Wed, 18 Apr 2001 15:48:20 GMT Organization: Verio Xref: supernews.google.com comp.lang.ada:6974 Date: 2001-04-18T15:48:20+00:00 List-Id: On Tue, 17 Apr 2001, Ayende Rahien wrote: > "Brian Rogoff" wrote in message > > On Tue, 17 Apr 2001, Larry Hazel wrote: > > > Ayende Rahien wrote: > > > > > > > > Why use it like this? > > > > for (;Iter.hasNext(); X = Iter.next() ){ > > > > //do stuff > > > > } > > > > > > > > Is much more readable, IMO. > > > > > > I disagree. The C style for loop is totally unreadable garbage IMO. > > > > Well, to a C programmer the for loop is perfectly readable. As someone > > familiar with C, Java, Ada, and a few other languages, I have to say that > > I found Tucker's original expression far preferable to Ayende's, > > though I'd prefer "hasMore" to "hasNext" :-). Ayende, why do you find the > > for loop preferable in this case? > > Because it iterate, which is the whole point of the for loop. What's the point of the while loop? No, don't answer that, or this thread will loop on and on :-). Suffice to say, I disagree. The following is fine while (iter.hasMore()) { X := iter.getNext(); // do stuff with X } while; // optional :-) > In this case, the code that decide how many times the loop will loop is on > one line, and easy to see & understand. > The part that does stuff is seperated from the looping part, which makes for > easier reading. > I tend to use the for loop whenever I've something that can be broken like > this (checking & forwarding). I tend to use it for traversing arrays. I think of a lot of these iterators as data sources. If you don't know how long a data source is then it makes more sense to query for the data; if I were to express the operation in (American) English, my favorite programming language, I'd say "While this data source has data, do the followings : do some stuff, take the next data item out, do some more stuff, and then start over. > > As long as we're considering iterators, it should be mentioned that > > iterators are one of the great examples for adding downward funargs into > > the language. > > What is funargs? Functions as arguments. There are now funargs in Ada but they are C level function pointers. Personally I'd love to see downward and even anonymous funargs in Ada but I suspect it'll never happen. At some point in your programming career you'll have to program in a functional language (a Haskell or a Clean, or an ML like OCaml or SML, or even a Lisp) and if you do it enough you get used to programming with functions as first class data objects. > > Indeed, some people have a problem adapting to the features of other > > languages. > > In C's case, those stuff may reduce readiabilty to those who are unfamiliar > with the language, but they make for smaller code. > Of course, you can always overdo it and turn a sentece to incomprehincible > mess, which some people take advantage of. > I recall once having to read (my own, written couple of days ago) a 260 > characters of C++ code which took over an fifteen minutes to comprehend. > IIRC, it was copying one matrix to the other. As soon as I saw Ada it crystallized a lot of my thoughts on proper C style and I haven't been bitten by evil C in my own code for a while. Two simple things to help with your C: (1) Don't do flashy pointer indexing and use of ++ on pointers to traverse arrays. This is one of those tests they give to C programmers about writing strcmp using a pointer increment and checking for the null at the end of the string. Horrible. I'd write similar code using array notation and a for loop. (2) Don't use ++, +=, ... inside array indexings, function calls, etc. Just put them in a for loop or as independent actions on their own lines. Also, let me recommend that you pick up a copy of Hanson's "C Interfaces and Implementations" so that you start applying good style to your C. I'm actually a firm believer in writing Ada-like C code, but that doesn't mean "twist C surface syntax to look like Ada" but rather "apply the principles of SW engineering embodied in Ada to write C with interface and implementation well separated". -- Brian