Skip to content

Instantly share code, notes, and snippets.

@mathesond2
Created March 28, 2023 14:22
Show Gist options
  • Save mathesond2/1e5c3751ace31b6ab541ff8552141b7a to your computer and use it in GitHub Desktop.
Save mathesond2/1e5c3751ace31b6ab541ff8552141b7a to your computer and use it in GitHub Desktop.
Notes from "Putting An Age-Old Battle To Rest" by J. B. Rainsberger

Traditional rules of simple code design:

  1. pass its tests
  2. minimalizes duplication
  3. reveals its intent
  4. reduces the number of classes/modules/packages

^This is put in order of importance. Now some have argued over the order of 2 & 3, because when you're doing one you're doing the other...these 2 rules inform each other in a rapid feedback cycle.

what happens: when you focus on removing duplication, a new structure emerges.

"I like to say that if a solution wants to be structured a certain way, then removing duplication helps that structure to emerge."

"also, when I try to focus on improving names, Feature Envy and friends become more apparent, and the code makes it clearer that it wants to move to somewhere else. Similar concepts start calling out to each other across the codebase, hoping for the programmer to unit them in a single module. crowded, unrelated concepts struggle to get away from one another. Responsibilities gradually move to a more sensible part of the codebase"

Both of these contribute to higher cohesion

  1. remove duplication = a structure emerges
  2. improve names = redistribute responsibilities

But heres the trip: when you create a new structure, you'll need to name it something. And over time, you'll need to improve that name. As the names improve, cohesion improves, so that similar things move closer and different things move further away.

Removing duplication creates "buckets", and creating better names further redistributes things among these buckets. Over time, the rest of the system - the client of these buckets - treat the buckets like single things rather than collections of things. This is Abstraction.

Now at this higher level of abstraction: we move away from the details in the buckets and eventually start seeing new patterns in bigger things (above the initial bucket level)...this means more duplication. we see this duplication, then we remove it, with a new structure. That new structure needs a name, and over time we have to improve those names. In the process, more details slip away and other abstractions emerge. This raises the level of abstraction again.

when we remove duplication, we create buckets. we improve those buckets' names over time, creating more cohesive, easily-abstracted buckets.

Recap:

  1. remove duplication
  2. structures emerge
  3. name structures
  4. improve names
  5. abstractions emerge
  6. higher level duplication occurs

^ and rinse, repeat.

Remove duplication and improve names for high cohesion and to organize code into helpful layers of beneficial abstractions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment