I have always been interested in tiny code–the clever solution that seems too small to work.

In the 1970s, Atari game programmers crammed games into 1 or 2k cartridges. They used tricks that would never occur to us today, like scanning through the binary code data to see if any of the code could be repurposed (perhaps with a few tweaks) to be used as graphics. For the Atari Chess game, they went scanning through the logic code to try to see if any strings of bytes looked like a rook, or a bishop.

Another trick was to write your machine language code so that it did one thing if you executed the code starting at one byte, and another thing if you jumped in at the next byte. (Remember the processor was the 6502. Instructions were either one or two bytes long. Writing code interleaved in such a way was a puzzle, but could pay off in a big way if it meant the difference between a 1k ROM and a 2k ROM game cartridge.)

As computers got more powerful, the things you could do in 1k of code got more impressive. The 1k competitions for multimedia computers like the Amiga and the Atari ST were amazing, sometimes featuring 3D objects whirling about over starfields.

(We game programmers always wondered if the guys who won these competitions could write a decent videogame. That’s how we judged whether you were a good programmer or not. I don’t think we ever found out.)

But Seriously…

Aside from the “demo scene,” there have always been more serious programmers who admired pithy one-liners. Anyone who really digs APL or Perl is a candidate.

Yeah, Mine’s Big

We rarely think about code size any more. Our code is dwarfed by the multimedia assets we pull in. Does it matter how big your JavaScript is? It does matter, because the JavaScript code is being served over and over and over, but mostly you want it to work right, and you can get so wrapped up in the nightmares of compatibility that it’s easy to ignore your code size. “I’ll just gzip it later” is the universal thought.

This clever bit of metaprogramming turns a bunch of Canvas functions into functions that can be chained (also known as a cascaded). I don’t know if p01 came up with this trick, or whether he stumbled upon it somewhere else, but it’s a great idea. It lets him perform a whole series of operations in one line.

That whole bit counts as one line of JavaScript (line 15, as you can see by the comment), for the sake of this contest (it would be uglier on one physical line, but it’d work).

Users of the jQuery library are used to this chaining. It works if you return this instead of letting the default value undefined be returned.

Next time I use Canvas, I’m stealing this trick.

—

True story.

When I took Numerical Methods in college, our APL programs were graded based on the number of characters in the program. This was hardcore APL, on a timeshare DEC computer with dedicated APL Greek-letter keyboards and special video terminals that could display the Greek APL symbols (this was back in the days of monochrome text displays–before graphics terminals were common).