AHA programming - Kent Dodds

I watched Kent Dodds presentation in React Summit 2020 and also read his aha blog post.

AHA stands for Avoid Hasty Abstractions and Kent likes it described as: prefer duplication over the wrong abstraction.

Ok, on the surface that sounds controversial, prefer duplication?
Goes against principle of DRY.
But we already follow guidelines that cause duplication e.g. WET Write Everything Twice ...but not a third time (we follow that at Opentable).
Often times you think you know a component will be reused but in fact you may be wrong and it may take a couple of implementations to really understand if it's common pattern or not. 

And that's Kents point: don't rush into abstractions.
Code duplication is ok while it's being figured out. Over time/uses the commonalities will become clear and then you can create an abstraction that will work better. He's not saying don't do abstractions, he is saying take your time to learn the domain.

In the video presentation Kent highlights concerns with premature abstractions:

  1. Abstractions can evolves past the original use case and supports use cases that don't relate much to each other any more
  2. Because abstraction exists then we end up using them even when it may no longer be appropriate.
  3. Tests get created that exist for tests only. Increases maintenance cost and people are not confident removing
This is legit. We've all looked at a codebase and struggled to understand one or more components or functions with lots of parameters and branching logic and scratched our heads. That is probably a code smell that the abstraction may no longer fit all use cases. It's also possible some of those original use cases may no longer exist either.

Read Sandi Metz, "all the little things" and "The Wrong Abstraction" which  explains how to back out of such scenarios:

  1. go though all uses and replace with inline or custom code instead of shared abstraction (re-introduce duplication basically)
  2. then you can really see similarities and what is and isn't common
  3. now you understand if an abstraction is still relevant and if so what it should be

Reminds me of a refactoring like you'd see in Martin Fowlers refactoring catalog

Kent finished the video with some parting nuggets

  • Duplication is far cheaper than the wrong abstraction, so prefer duplication over the wrong abstraction
  • Wait for commonalities in duplicate code to scream at you for abstraction
  • DRY is not necessarily a bad thing
  • if you have shared code with lots of branches then resist the urge to add more conditionals to it. Refactor it first. Learn about the callers of the abstraction so you understand what's needed


When starting on a new codebase, Kent advises take an iterative approach, don't obsess about duplication. Don't try architect app for something it's not today.

"There are no hard and fast rules. It's nuanced."

Comments

Popular posts from this blog

deep dive into Material UI TextField built by mui

angular js protractor e2e cheatsheet

react-router v6.4+ loaders, actions, forms and more