Is this a dream or a nightmare? (Was Re: XML)

Alex Martelli aleaxit at yahoo.com
Sun Oct 8 02:57:13 EDT 2000


"David T. Grove" <pete at petes-place.com> wrote in message
news:39dfe6db.42535595 at news.davesworld.net...
    [snip]
> (So, if you don't see that C++ as extremely verbose, then you are
> either a careless programmer who makes incomplete black boxes, a
> newbie who doesn't know how to make black boxes, or you have an
> incredible set of already-defined superb and complete black boxes that
> I want... gimme gimme gimme.)

You're welcome to take that "incredible set of boxes": it's called "the
standard C++ library" (many call part of it "STL", but that is not
quite right!), and is a good part (but just a part) of what makes a
total mockery of your absurd claim that C++ takes 4 times more
lines of code than C to complete typical tasks.

I have quite a few more "incredible set of boxes", that were made
for me by other people, such as Julian Smart's wxWindows, Microsoft
ATL, and have very similar effect for their respective application
fields -- an order of magnitude LESS code and effort is needed in
C++ than it would be in C.  C++ supplies the right machinery for
this purpose, particularly generic-programming (templates).

I have, of course, also written my own "sets of boxes" (which are
used by hundreds of other programmers in my firm -- unsurprising,
since "base software and tools" has been one of my main roles as
a "senior software consultant" around here), but that's just "more
of the same".

Let's just focus on what comes with the standard language, to show
how silly and absurd your 4-to-1-in-favour-of-C claim really is.  The
classic task: line-number index for whitespace-separated words.  A
similarly classic approach, with no special shortcuts or anything.

// import needed functionality

#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    typedef map<string,vector<int> > map_t;
    map_t themap;

    // input the data
    int linum=0;
    string line;
    while(getline(stdin,line)) {
        ++linum;
        istringstream sline(line);
        string word;
        while(sline>>word)
            themap[word].push_back(linum);
    }

    // output the results
    for(map_t::iterator it = themap.begin();
         it != themap.end(); ++it) {
            cout << it->first << ':';
            for(vector<int>::iterator x = it->second.begin();
                 x != it->second.end(); ++x)
                     cout << ' ' << *x;
            cout << '\n';
    }

}

The SLOC:
    7    "import needed functionality"
    5    "main declarations & closing brace"
    9    "input the data"
    8    "output the results"

for a total of 29.  So, where's the 7.25-line C program (using only
functionality distributed with the language) that does the same? I'll
be genererous and let you use up to 8 SLOC...


Here's the Python equivalent:

import fileinput

themap = {}

for line in fileinput.input():
    for word in line.split():
        themap.setdefault(word,[]).append(fileinput.lineno())

words = themap.keys()
words.sort()

for word in words:
    print word+":",
    for linum in themap[word]:
        print linum,
    print

The SLOC:
    1    "import needed functionality"
    1    "main declarations"
    3    "input the data"
    2    "sort the results"
    5    "output the results"

for a total of 12.  Where's the 0.48-line equivalent perl program, by
the way?


There may, or may not, be something worth discussing in some of your
more generic considerations that follow.  But until and unless you climb
down totally from your ridiculous earlier numerical claims (and I hold
they were so _obviously_ crazy that an apology for wasting the readers'
time would not be wasted:-), particularly the one about C++ taking 4
times as many SLOC for the same task as C, I think there's a "credibility
gap" that is just too wide to warrant such discussion.


Alex






More information about the Python-list mailing list