SV: Python Productivity over C++
Thomas Wouters
thomas at xs4all.net
Tue Jun 13 18:40:16 EDT 2000
On Wed, Jun 14, 2000 at 08:55:44AM +1000, Neurocrat wrote:
> for (int i = 0; i < 10; ++i);
> cout << "Hello" << endl;
>
> This, of course, prints "Hello" only once, not 10 times as we
> intended. In a toy example it's easy to spot the offending ; at the
> end of the loop construct, but is there a C or C++ programmer alive
> who hasn't spent hours trying to track down insidious problems caused
> by that single legal semicolon? I know I have.
>
> If I made the corresponding mistake in Python:
> for x in range(10) # forgot the :
> print "hello"
> I'd know it straight away.
This reminds me of a funny piece of code I saw not so long ago. A year or so
ago I inherited a codebase for a MUX, a Multi User Experience (similar to a
MUSH, which is similar to a MUD, a Multi User Dungeon ;) This particular MUX
is the engine for an online game of Battletech. It's also code that has been
around for a long, long time, and has been hacked by literally dozens of
people, either directly or indirectly.
A couple of years back, someone hacked in a new feature. Instead of always
displaying the same 'welcome' message before a login, the MUX now chose one
at random from a directory of such files. While I was doing a major cleanup
of the whole MUX, a couple of months back (and fixing a *lot* of bugs, by
the way) I came into the vicinity of that piece of code because of an unused
variable, and this is what I saw:
#if 0
/* Done this way IF things worked right */
while (1)
{
if (!(de = readdir(d))) break;
if (de->d_name[0] == '.');
continue;
if (!strstr(de->d_name, ".txt"))
continue;
if (*cnt == max) continue;
sprintf(buf, "%s/%s", dir, de->d_name);
fcache_read(&(foo[*cnt].fileblock), buf);
(*cnt)++;
}
closedir(d);
#else
sprintf(buf, "perl -e 'opendir(FOO,\"%s\");@files=readdir(FOO);foreach (@files) { if (/^[^\\.].*\\.txt$/) { print \"%s/$_\\n\";}; };closedir(FOO);'", dir, dir);
f = popen(buf, "r");
while (!feof(f))
[ ... code to read the pipe ... ]
#endif
I kid you not, this feature, which had been in for a couple of years,
running between 2 and oh, 5, public MUXes and probably lots more private
ones, at any one time, started *perl* to read a directory contents. And why?
Because the code that should've worked, using opendir() and all, didn't, and
the author itself (*not* one of those other thirty-odd hackers) couldn't
figure out why it didn't work.
Of course, none of the people who could have written that dare admit it now
;) so the real doofus will remain anonymous forever. I have to admit, too,
though, that I wouldn't have caught it that fast if it wasn't for 'indent'.
But then, that's why there are tools like that ;)
--
Thomas Wouters <thomas at xs4all.net>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
More information about the Python-list
mailing list