tech

Little Ways Code Works

There are some principles that are valid across disciplines. Biology, geology, chemistry. And code. Yes, code. Here is what science teaches you about writing code.


Sandro Maglione

Sandro Maglione

Software

I just read an essay titled "Little Ways The World Works".

If you find something that is true in more than one field, you’ve probably uncovered something particularly important.

The essay explores some universal (messy) principles. Here is how those principles apply to your code 👇


Muller’s ratchet

Have you ever onboarded a new junior developer? New job, new codebase, little experience.

Guess how his first PR looks like? Easy. Copy and paste code/patterns.

It "makes sense". But what if those patterns were introduced in a rush by some other developer a while ago? And, surprise, they are!

Muller’s ratchet: Dangerous mutations tend to pile up when there’s no genetic recombination, ultimately leading to extinction.

The price of "shortcuts" in code is proportional to the number of developers working on the codebase (especially junior).

Dangerous mutations (I am looking at you useState/useEffect), without genetic recombination, ultimately lead to extinction 😬

"Genetic recombination" in code means refactoring 💁🏼‍♂️

Cope’s Rule

Simple is safe. Especially in code.

But simple and powerful is the hardest balance to keep. Each new feature piles up new utility functions, new tests, new packages.

That's expected. A "mature" codebase gives vibes like "big", "powerful", "complete". But also "close to extinction".

Cope’s Rule: Species evolve to get bigger bodies over time, because there are competitive advantages to being big.

But big has its own drawbacks, and can often be the cause of extinction.

Every line of code is a liability. The best line of code is the line never written. Aim for simple and small (as much as possible).

Ah, and prune unused code (knip).

Cromwell’s rule

How many times have you seen/written this line of code?

switch (value) {
  case 1:
    return "a";
  case 2:
    return "b";
  case 3:
    return "c";
  default:
    // This should never happen!
    throw new Error();
}

Cromwell’s rule: Never say something cannot occur, or will definitely occur, unless it is logically true (1+1=2).

Code is plagued by states that "should" not be possible, but they eventually become reality.

The key is the word "should". Something "should never happen", but with code any kind of unpredictable combination will occur, and your "should" becomes a "will": "it will happen".

Unless it is logically true! 🙌

With code, "logically true" is the role of types.

1+1=2 is logically true. () => number makes the statement "the function returns a number" logically true (more or less, thanks javascript and as 🫠).

Liebig’s law of the minimum

Performance is tricky, and therefore also a topic that people like to discuss a lot.

Everyone throws benchmarks for every library, every framework. You see it everywhere. But:

Liebig’s law of the minimum: A plant’s growth is limited by the single scarcest nutrient, not total nutrients.

No matter how "fast" React is, if your codebase fetches 300Mb of data, all is slow.

As a rule of thumb, avoid optimization until needed.

Three Men Make a Tiger

If one person tells you there’s a tiger roaming around your neighborhood, you can assume they’re lying.

If two people tell you, you begin to wonder.

If three say it’s true, you’re convinced there’s a tiger in your neighborhood and you run for your life.

This principle needs a slight change in the era of social networks. "Person" is too generic on the web. It may be a dog, an AI, a bot.

You need a more stringent check for the definition of "person" online (and number of followers is not it).

Trust people that you know in person, and those who they recommend.

"If three say it’s true, you’re convinced there’s a tiger". That's also how every library or framework goes viral 💁🏼‍♂️


As more and more people (and companies) dive into effect, the ecosystem is also going to expand and evolve.

If you want to be part of this, you know where to start.

See you next 👋

Start here.

Every week I dive headfirst into a topic, uncovering every hidden nook and shadow, to deliver you the most interesting insights

Not convinced? Well, let me tell you more about it