swyxio / swyxdotio

This is the repo for swyx's blog - Blog content is created in github issues, then posted on swyx.io as blog pages! Comment/watch to follow along my blog within GitHub
https://swyx.io
MIT License
335 stars 45 forks source link

Preemptive Pluralization is (Probably) Not Evil #295

Closed swyxio closed 2 years ago

swyxio commented 2 years ago

source: devto devToUrl: "https://dev.to/swyx/preemptive-pluralization-is-probably-not-evil-1mp3" devToReactions: 11 devToReadingTime: 6 devToPublishedAt: "2021-03-12T23:02:49.409Z" devToViewsCount: 197 title: Preemptive Pluralization is (Probably) Not Evil published: true description: What if we just assumed we might have two of everything? tags: Reflections, Programming, Advice slug: preemptive-pluralization canonical_url: https://www.swyx.io/preemptive-pluralization cover_image: https://i.ytimg.com/vi/INzAc4iI5Zo/maxresdefault.jpg disclosure: This is an idea I have been mulling for a while, but have not practiced at scale. Written in a couple hours after a prompt from a podcast.

See discussions on Twitter, Reddit, Lobste.rs, and my mixtape

Before you write any code — ask if you could ever possibly want multiple kinds of the thing you are coding. If yes, just do it. Now, not later.

A few common examples to illustrate:

You can listen to Ben Orenstein of Tuple discuss this on my mixtape:

I've done this refactoring a million times. I'll be like, I thought there would only ever be one subscription team, user plan, name, address , and it always ends up being like, "Oh, actually there's more." I almost never go the other way. What if you just paid the upfront cost of thinking "This is just always a collection"?

Donald Knuth is famous for noting that Premature Optimization is the Root of All Evil (there's some nuance to that, btw). I am very sympathetic to the appeal to simplicity — if "You Ain't Gonna Need It", then don't use it. But I think Preemptive Pluralization — projecting forward into hypothetical situations when you need N types of a thing — is exempt, even though you are literally optimizing for a future you don't currently live in.

It is a LOT easier to scale code from a cardinality of 2 to 3 than it is to refactor from a cardinality of 1 to 2. This is a fundamentally under-appreciated nonlinearity. In other words, Preemptive Pluralization can make the difference between "sure, I'll add that today" and "this is going to take us 2 months and we'll introduce merge conflicts with every other in-progress feature."

Write Robust Code

Requirements volatility is a core problem of software engineering. As a software engineer, writing code that does what you ask of it today is the bare minimum. Your real skill comes in what happens next — what you do when requirements inevitably change, whether by new feature request or scaling issues arising from I/O or compute bounds.

It may not be enough to write code for what you foresee in the near term — those are just more requirements. Software design and architecture is all about making it easy to respond to unforeseen changes.

Hillel Wayne has proposed calling these requirement perturbations. If a small, typical feature request can throw your whole design out of whack, then you have fragile code. Clearly you want the opposite of fragile — I am tempted to call it "Antifragile" because that gets clicks — but really the best you can hope for is code that mostly doesn't fall apart due to 1-2 standard deviation changes in requirements. In other words: robust code. Robust code is optimized for change (more in a future blogpost).

The nonlinearity in how expensive it is to make a change comes from the "emergent sclerosis" of code. Code that is robust to future changes is far cheaper to write today, than when written later, as delayed technical debt that you must pay up before you can proceed to a feature request. Fragile code is like the payday loan lender of technical debt.

Preemptive Pluralization creates Robust Code.

Is it Even Premature?

I'm so committed to not prematurely optimizing that I want to make a final pitch for why Preemptive Pluralization is not premature.

Let's address obvious criticisms of Preemptive Pluralization:

Ultimately I think what makes something premature or not is your definition of what you need to write:

Just make Robust Code a design requirement from the start and then figure out what that means :)

Related Preemptive Moves

More Pluralization Points

More awkward things to pluralize:

More from @nivertech on Twitter:

Acknowledgements and Further Reading

swyxio commented 2 years ago

This topic was revived again today by Ben Orenstein on Twitter.

osi-jehrlich commented 2 years ago
- Preemptive Pluralization is (Probably) Not Evil
+ Preemptive Pluralizations are (Probably) Not Evil