HACKER Q&A
📣 superzamp

What's the most critical piece of code you have ever written?


What's the most critical piece of code you have ever written?


  👤 paulrpotts Accepted Answer ✓
Since getting into embedded firmware, I've had to write some very "critical" pieces of low-level code, often very low-level.

For example, I took over a piece of code that ran on a DSP on an audio card. This code handled PCI bus transfers and I needed to change the shot size and optimize it - a lot. So, parts became Motorola DSP assembly language. I had to find a lot of tricks, using unused bits in wide registers as cycling counters, fun stuff like that. But I got it working and it became very reliable.

I've written low-level code for other DSPs, particularly code that handles audio mixing, does filtering and sample-rate conversion, and things like that.

I also have written low-level driver code for various embedded processors in C, including code that drives SPI peripherals.

One of the more interesting challenges is writing driver code that allows writing to on-chip flash memory. The code is executing right out of flash memory, but then you need to program pages from a write buffer into the same flash memory. To do this you have to jump to a function that is guaranteed to be in RAM, change the chip configuration such that the memory mapping behaves differently, write the page, put the chip configuration back to normal, and then continue executing the out of flash.

Other fun "critical" code includes bootloaders that upload a new version of the firmware, and it's interesting what they have to do, to then start executing the firmware. They have to stop interrupts including clocks, set some critical registers, and then NOT re-enable interrupts, but take a "soft reset" and launch the new program, which then will re-enable interrupts after it gets itself set up.

I really enjoy this kind of low-level stuff, which I guess makes me an outlier, but on the other hand it's been good for my career to carve out a niche doing stuff that a lot of people don't want to do.

Oh - for a slightly different definition of "critical," I've also been employed writing test code for medical devices (and found bugs in medical device firmware this way).


👤 danbolt
I worked at a company that was contracted to do an expansion for a computer game called Age of Empires II. It was more or less my first job out of university, and I was tasked with expanding the UI and JSON file loading so people could make maps and art for custom campaigns. I wasn't very familiar with the game before starting to work on it, so I was kind of going in blind.

It wasn't "critical" in the sense that it it required a high service uptime or anything, but I've seen people create lots of neat little stories and scenarios and share them online for other people to try. [1][2] It's fun to have worked on something that let people express themselves.

[1] https://steamcommunity.com/sharedfiles/filedetails/?id=13839...

[2] https://steamcommunity.com/sharedfiles/filedetails/?id=19281...


👤 muzani
On my startup, I realized I had hundreds of orders a day after launch and no system to handle them properly (I expected about 3 orders max). There was no shopping cart; it was just users ordering several items separately and I had to match them by some common identifier like phone number.

Within hours, I hacked together an app that would assemble those orders into a form of a cart, and then copy-paste text. Then we'd SMS or WhatsApp the person ordering them to confirm the order.

The system would also track delivery status, payment status (no payment gateway, all cash), and whether the order arrived. It had a remarkable amount of responsibility and small margin of failure for something built in less than a day.


👤 programmerslave
I wrote some code that runs core peices of Amazon.com, millions of tps

👤 giantg2
I would guess all my code relating to managing millions of investors' money. Specifically an enterprise-wide upgrade that I was involved in since other teams had to cut over at the same night as us. I even found a bug in the migration playbook that I was able to fix and alert other teams about. Too bad it wasn't appreciated.

👤 dave_sid
I once wrote some Perl code that worked out the difference between two times on two servers. Actually it wasn’t really that critical at all.

👤 cblconfederate
Heh, a tiny python program to drive the device that delivers an electric shock to lab mice.