Log in

No account? Create an account

Wonderful Life, Lucky Code, Open Source

« previous entry | next entry »
Jan. 18th, 2007 | 01:11 pm

Yesterday I spent a few hours going over the internals of MySQL with
the Falcon team.

At some point we hit the "and why does that work like that?".

It was because of copy and paste, or as I think of it in code as
"evolution by the lucky". When code is well documented, encapsulated,
or just plain understandable one developer will copy and paste it to
another section of the code.

Now where does the lucky part come it? My description sound more like
survival of the fittest?

Well now let us insert reality. Sometimes someone copies and pastes
code because they believe it is doing what they want, and they don't
always get it right. Other times there is a flaw in the code that
isn't found for some time. Survival of code occurs because of luck.

In a single code base you can solve this with libraries. Instead of
copy and pasting code, reduce the logic into some reusable function
or method.

In the open source world, this may span projects. I remember years
ago looking at a problem in performance for a customer in their ftp
server and they were using Proftpd. What did I discover? They were
running into a similar problem that had been solved before in Apache.
What is the common denominator? Proftpd had copied the Apache
ap_malloc code.

Copy and paste back in the code from Apache.

Easy fix :)

The good thing is that today the Apache malloc code can all be found
in a library that is a standalone component from Apache called the APR. So anyone who wished to adopt
the malloc calls today could just use the library.

But this is only one case, look around, you can find more.

I once used LibaryMagic because "everyone else did". I didn't realize
that it made a system() call to another application to do PDF's. That
turned out to be "no so good".

A resolution? Don't reuse code? No, do, but study it beforehand. Make
sure you know how the library or snippet of code works. Don't just
copy and paste and assume the last guy was right. When you find a
problem don't just solve it for your case, provide patches.

Find where the code exists and replace it there as well.

Make the code a bit more lucky.

Apologies to the memory of Stephen J. Gould to reducing his concepts
down to "survival of the lucky". If you haven't read any of his books
I would recommend them. I find that evolution as a theory finds its
way into many natural aspects of the world, including the creativity
found in developing code.

Link | Leave a comment | Share

Comments {2}

Lover of Ideas

(no subject)

from: omnifarious
date: Jan. 18th, 2007 09:35 pm (UTC)

I have a tendency to call programming with cut & paste "cargo cult programming". This is because often the person cutting and pasting the code only has the vaguest idea of what it does.

The most visible aspect of this is when you see some silly construct repeated everywhere when it only makes sense in one particular location. The construct got needlessly and pointlessly copied because the whole code block it was in was considered magical and people kept cutting and pasting it all over to get some similar piece of functionality.

Reply | Thread


(no subject)

from: gilraen
date: Jan. 18th, 2007 11:22 pm (UTC)

Stephen J Gould always makes me think of you :)

Reply | Thread