comp.lang.ada
 help / color / mirror / Atom feed
From: Brian Rogoff <bpr@shell5.ba.best.com>
Subject: Re: Problem trying to implement generics.
Date: Wed, 18 Apr 2001 15:48:20 GMT
Date: 2001-04-18T15:48:20+00:00	[thread overview]
Message-ID: <Pine.BSF.4.21.0104180808360.16514-100000@shell5.ba.best.com> (raw)
In-Reply-To: <9bi7vb$9j7$1@taliesin.netcom.net.uk>

On Tue, 17 Apr 2001, Ayende Rahien wrote:
> "Brian Rogoff" <bpr@shell5.ba.best.com> 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





  reply	other threads:[~2001-04-18 15:48 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-11 15:04 Problem trying to implement generics Ayende Rahien
2001-04-12  1:41 ` tmoran
2001-04-12 13:15   ` Ayende Rahien
2001-04-12 18:15     ` tmoran
2001-04-13 11:18       ` Ayende Rahien
2001-04-13 10:35         ` chris.danx
2001-04-13 11:54           ` Ayende Rahien
2001-04-13 11:49             ` chris.danx
2001-04-13 23:03               ` Ayende Rahien
2001-04-13 23:01                 ` Robert A Duff
2001-04-14  0:05                   ` Brian Rogoff
2001-04-14  1:12                     ` Ayende Rahien
2001-04-14  1:44                       ` Brian Rogoff
2001-04-14 14:03                         ` Dmitry A. Kazakov
2001-04-14 16:30                           ` Ayende Rahien
2001-04-14 16:28                             ` Michael Erdmann
2001-04-15  3:27                             ` James Rogers
2001-04-15 12:20                               ` Ayende Rahien
2001-04-15 14:09                               ` Dmitry A. Kazakov
2001-04-15 18:22                                 ` tmoran
2001-04-15 13:48                             ` Dmitry A. Kazakov
2001-04-15 20:44                               ` Ayende Rahien
2001-04-16 14:34                                 ` Dmitry A. Kazakov
2001-04-14  1:33                     ` Robert A Duff
2001-04-17  8:50                     ` Jean-Pierre Rosen
2001-04-17 13:20                   ` Tucker Taft
2001-04-17 16:51                     ` Ayende Rahien
2001-04-17 17:16                       ` Larry Hazel
2001-04-17 18:11                         ` Brian Rogoff
2001-04-17 19:10                           ` Marin David Condic
2001-04-17 21:08                             ` Brian Rogoff
2001-04-18 15:16                               ` Chad R. Meiners
2001-04-18 16:33                                 ` Marin David Condic
2001-04-17 21:09                             ` chris.danx
2001-04-17 21:11                             ` chris.danx
2001-04-17 21:17                             ` chris.danx
2001-05-08  5:40                             ` Lao Xiao Hai
2001-05-11  9:43                               ` John English
2001-05-12 19:16                                 ` Lao Xiao Hai
2001-04-17 19:32                           ` Larry Hazel
2001-04-17 21:03                           ` Ayende Rahien
2001-04-18 15:48                             ` Brian Rogoff [this message]
2001-04-20 12:34                               ` Georg Bauhaus
2001-04-20 12:42                                 ` Lutz Donnerhacke
2001-04-20 12:45                                 ` Lutz Donnerhacke
2001-04-20 19:48                                 ` Brian Rogoff
2001-04-20 20:36                                   ` David Starner
2001-04-20 23:02                                   ` Robert A Duff
2001-04-23  2:45                                     ` Brian Rogoff
2001-04-24  1:15                                       ` Robert A Duff
2001-04-24  2:00                                         ` Brian Rogoff
2001-04-24 15:12                                           ` Georg Bauhaus
2001-04-24 15:09                                         ` Georg Bauhaus
2001-04-24 18:36                                           ` Marius Amado Alves
2001-04-19 13:08                           ` Larry Kilgallen
     [not found]                           ` <9bi4g4$97m$1@nh.pace.Organization: LJK Software <YlSyXUaQmD+$@eisner.encompasserve.org>
2001-04-19 14:20                             ` Marin David Condic
2001-04-18  5:34                       ` Mike Silva
2001-04-18 16:55                       ` Ray Blaak
2001-04-24 16:00                       ` Tucker Taft
2001-04-12 13:57 ` Andy
2001-04-13  6:34   ` Simon Wright
2001-04-13 11:11   ` Ayende Rahien
2001-04-12 18:06 ` Stephen Leake
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox