login
Header Space

 
 

Re: [RFC] Convert builin-mailinfo.c to use The Better String Library.

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <git@...>
Date: Friday, September 7, 2007 - 4:15 am

David Kastrup wrote:

I like to phrase that a slightly different way: anyone can make 
something complicated, but it takes genius to make something simple.

A very big goal for D is to make what should be simple code, simple. It 
turns out that what's simple for a computer is complex for a human. So 
to design a language that is simple for programmers is (unfortunately) a 
rather complex problem. Or perhaps I'm just not smart enough <g>.

A canonical example is that of a loop. Consider a simple C loop over an 
array:

void foo(int array[10])
{
     for (int i = 0; i < 10; i++)
     {   int value = array[i];
         ... do something ...
     }
}

It's simple, but it has a lot of problems:

1) i should be size_t, not int
2) array is not checked for overflow
3) 10 may not be the actual array dimension
4) may be more efficient to step through the array with pointers, rather 
than indices
5) type of array may change, but the type of value may not get updated
6) crashes if array is NULL
7) only works with arrays and pointers

Since this thread is talking about C++, let's look at the C++ version:

void foo(std::vector<int> array)
{
   for (std::vector<int>::const_iterator
        i = array.begin();
        i != array.end();
        i++)
   {
     int value = *i;
     ... do something ...
   }
}

It has fewer latent bugs, but still:

1) type of array may change, but the type of value may not get updated
2) too darned much typing
3) it's more complicated, not simpler

Frankly, I don't want to write loops that way. I want to write them like 
this:

void foo(int[] array)
{
   foreach (value; array)
   {
     ... do something ...
   }
}

As a programmer, I'm specifying exactly what I want to happen without 
much extra puffery. It's less typing, simpler, and more resistant to bugs.

1) correct loop index type is selected based on the type of array
2) arrays carry with them their dimension, so foreach is guaranteed to 
step through the loop the correct number of times
3) implementation decides if pointers will do a better job than indices, 
based on the compilation target
4) type of value is inferred automatically from the type of array, so no 
worries if the type changes
5) Null arrays have 0 length, so no crashing
6) works with any collection type

[This example is extracted from a presentation I've made.]

------
Walter Bright
http://www.digitalmars.com  C, C++, D programming language compilers
http://www.astoriaseminar.com  Extraordinary C++

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: [RFC] Convert builin-mailinfo.c to use The Better String..., Johannes Schindelin, (Fri Sep 7, 6:21 am)
Re: [RFC] Convert builin-mailinfo.c to use The Better String..., Johannes Schindelin, (Fri Sep 7, 6:56 am)
Re: [RFC] Convert builin-mailinfo.c to use The Better String..., Walter Bright, (Fri Sep 7, 4:15 am)
Re: [RFC] Convert builin-mailinfo.c to use The Better String..., Johannes Schindelin, (Fri Sep 7, 6:26 am)
Re: [RFC] Convert builin-mailinfo.c to use The Better String..., Johannes Schindelin, (Fri Sep 7, 6:28 am)
Re: [RFC] Convert builin-mailinfo.c to use The Better String..., Johannes Schindelin, (Thu Sep 6, 8:08 am)
speck-geostationary