wow this code looks like shit, we should rewrite

~ every new engineer on your team

You’re almost never going to hear an engineer say that somebody else’s code looks good. But why is code running in production always suboptimal?

Path dependence, my friend.

Complex systems aren’t a snapshot in time. They evolve. How you got here matters as much as what you were building.

๐Ÿคจ

Let’s say you’re The Evolution and your job is to design a mammal. One of the things mammals do is make noise.

They make noise using the larynx. An organ in their throat.

To make this larynx effective you need a high bandwidth low latency connection to the brain. The lower the latency, the faster animals can change sounds. The higher the bandwidth, the more complex their sounds can be.

Brain and throat are pretty close so you make a straight connection, right?

Makes sense to me.

And that’s not what real evolution did.

Evolution used the stupidest possible design for the recurrent laryngeal nerve connecting your larynx to your brain. It wraps around your heart first.

That’s right. A nerve going from your throat to your brain goes aaaalll the way down to your heart, wraps around your aorta, then piggybacks on the vagus nerve back up to your brain.

This suboptimal nerve is the only way you can move your vocal cords. In giraffes it’s so long there’s significant delay they need to compensate for.

facepalm giphy

How path dependence works

The extreme detour of the recurrent laryngeal nerves, about 4.6 metres (15 ft) in the case of giraffes, is cited as evidence of evolution, as opposed to Intelligent Design. The nerve’s route would have been direct in the fish-like ancestors of modern tetrapods, traveling from the brain, past the heart, to the gills (as it does in modern fish).
Over the course of evolution, as the neck extended and the heart became lower in the body, the laryngeal nerve was caught on the wrong side of the heart. Natural selection gradually lengthened the nerve by tiny increments to accommodate, resulting in the circuitous route now observed.

In sauropod dinosaurs the nerve was up to 28 meters (92ft) long ๐Ÿ˜ณ

Quite a design flaw.

And this all stems from the fact that every complex system is a function of today’s requirements and yesterday’s design.

Click through for source

Apply that recursively …

Click through for source

Keep going all the way back to the very first decision ever made on your project. That’s what you’re dealing with.

That’s why America is working on self-driving cars but not public transit. ๐Ÿ˜‰

So what can you do?

Every live system you touch shows influence of decisions made months and years ago. Often by people who don’t even work here anymore, had different constraints, and vastly different requirements.

Live systems are suboptimal. Always.

You’d do it differently today. Different technology. More experience. Different trends. Better knowledge about the problem.

But you can’t throw it out. It works. ๐Ÿ˜ฉ

First, take a breath. Relax.

Your crappy code is solving a problem. Right now. It really is. Regardless of how bad it looks. It’s working. Without this code running in production solving real problems, you wouldn’t have a job.

Second, realize that software can change.

Unlike with mammals, you can jump in there and change anything Right now. Make it completely different. You’ve got the power!

power giphy

So here’s what you do:

  1. Accept that the code works
  2. Look at current requirements (warning: this often involves supporting old requirements)
  3. If no code existed, how would you solve the problem?
  4. How much time/resources do you have?
  5. What can you do right now to get closer to 3?

The more you do 5 the more you’ll realize 3 was wrong.

So change 3 and keep going.

Do a little bit of 5 every time you touch a part of the system. As much as you can manage.

Eventually your code will look good to you and the next person to come along will say it’s crap.

Welcome to software gardening.

Cheers,
~Swizec

Learned something new? Want to improve your skills?

Join over 10,000 engineers just like you already improving their skills!

Here's how it works ๐Ÿ‘‡

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet ๐Ÿ“–right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

PS: You should also follow me on twitter ๐Ÿ‘‰ here.
It's where I go to shoot the shit about programming.