Embryology and Programming Languages

If you’ve never thought that the way molars develop in mammalian fetuses is way cool, you should read this article by PZ Mhieares at Pharyngula. It’s all about one substance in the environment of the developing jaw saying “you are going to become a bit of enamel”, and then that turns on another substance that tells neighboring cells, “cancel that: you don’t want to become enamel after all”, and stuff like that.

One fascinating thing about embryology is that the way living bodies develop is completely different from the way you’d build a house, or a credenza, or a sewing machine. It involves working in different media, with different tools, and that affects the way you do things, sometimes radically.

To me, the shift in thinking about building furniture to thinking about embryology is like learning a new programming language.

Read More

Pattern Substitution as Funky Iterator

I have a project in which I have a row of cells, and a number of segments of given lengths, and I need to try out all of the ways in which the segments can fit into the row. If you like, think of it as: how many ways can “eye”, “zygote”, and “is” be placed, in that order, on a row of a Scrabble board?

I’m doing this in Perl, so naturally I’d like to play to Perl’s strengths (pattern matching and substitution) rather than its weaknesses (arithmetic). And I’ve discovered a nifty little hack.

Read More

Calculating My Tastes

One thing you can do in iTunes is to give songs a rating from 1 to 5 stars. It also has an option to play highly-rated songs more often during shuffle play, but frankly, I can’t be bothered to go through my collection rating songs by hand. And besides, the metadata that iTunes stores for each song includes things like the number of times it was played, the last time it was played, the number of times it was skipped, the last time it was skipped, and the time when it was added to the library. It seems that from this, it should be possible to figure out what I like and what I don’t like. Specifically, it should be possible to write an AppleScript script that goes through the library and computes ratings.

Read More

Making Emacs Do Stuff at Startup

Like many users, I start an emacs session in .xinitrc and use it throughout the day. Since I’ve recently started using Emacs Planner, I wanted it to start up automatically in that first Emacs session (but not subsequent ones, if I just want to edit a file).

Read More

Soft Coding

Over at Worse Than Failure, Alex Papadimoulis has an article on soft coding. Hardcoding is when something is written in the code when it ought to be in a header constant or an external config file. Soft coding is the opposite, when something is put in a config file that really ought to be in the source. For instance, the number of days in a week isn’t likely to change any time soon, so you may as well just use “7” in your code. You might

#define DAYS_PER_WEEK 7
#define WEEKS_PER_YEAR 52
#define LOAN_DURATION DAYS_PER_WEEK * WEEKS_PER_YEAR

to make it clear what you’re calculating, but putting it in an external, user-configurable file is just absurd.

I’m guilty of softcoding, myself. In my experience, it comes from trying to be too generic. Code that’s too specific, too hardcoded, is hard to maintain and extend, so a good programmer will ask himself, “What requirements might change in the future? How should I write this so that when it changes, I can do it without too much pain?” This leads to things like declaring configuration constants at the top of a script, in a Makefile, or in a header file; or having the code read messages from an external file, rather than hardcoding them as strings.

But it’s also easy to overcompensate, and start making everything customizable, or overly generic. So it’s important to remember that at some point you have to actually make the code do something.

LJ Plugin for WordPress

I’ve made enough references to LiveJournal users like and enough that I got tired of copying and pasting the standard LJ HTML to link to them.

So I did what any good hacker would do, and wrote a WordPress plugin to expand <lj username> tags.

You can download it .

Removing Accents in Strings

I’ve been ripping and encoding a bunch of music. Since I’m a hacker, naturally I have scripts that take a file with artist, album title, and track titles, and finds the corresponding .wav or .aiff source files, encodes them as MP3 and tags them.

A lot of the music I have is in French or German (and some Spanish and Russian), so there are accented letters in names and titles. My input files are in UTF-8 format, so that’s cool. But one problem is that of generating a filename for the MP3 files: if I want to play the song “Diogène série 87” by H.F. Thiéfaine on his album “Météo für nada”, I don’t want to have to figure out how to type those accents in the file and directory names. I want the script to pick filenames that use only ASCII characters.

Read More

How Do You Spell the Names of the Months?

One construct that I’ve seen (and used) a lot in Perl is a hash that maps month abbreviations to numeric values that can be fed to POSIX::strftime:

our %months = (
    "jan" => 0, "feb" => 1, "mar" => 3, ...
);

This is useful for parsing log files and such. It works, it’s quick and easy, and it doesn’t require a whole tree of dependent modules (which are always on the wrong side of the Internet) to be installed.

But what’s always bugged me is that this is the sort of thing that the machine ought to know already. And besides, it’s US-centric: what if the person running the script is in a non-English-speaking country?

Fortunately, I18N::Langinfo knows the names of the months. Read More

Pirate Mode Code

I don’t know whether anyone noticed, but on Talk Like A Pirate Day, all the text on this site was converted to pirate lingo.

This was done through
that I consed up for the occasion. Feel free to steal download and use it.

When to Optimize

The First Rule of Program Optimization:
Don’t do it.

The Second Rule of Program Optimization (for experts only!):
Don’t do it yet.

— Michael Jackson

Read More