DRY is a coding acronym, it’s short for Don’t Repeat Yourself, and I have a bone to pick with it. Some of the most painful work of my career has revolved around tidying up the mess left behind by hardcore DRY practitioners.

It’s a useful mental model. It’s kind of like driving a car. You get up and running fastest by learning that one pedal makes you go, another makes you stop, and the wheel makes you turn. You don’t have to understand how an engine works, how brakes work, or how the car grips to the road. That mental model will serve you well to start with. Until you drive on an icy day. Then you push the pedal to stop, and you keep going.

DRY is the go-stop-turn of coding. It’s a valid, and useful model for thinking about code to begin with, but it’s a simplification of reality, and it can lead you in to a nasty accident too. The reason I’m writing about it is that I’m surprised, time and time again, to see it appearing on job descriptions — often for senior, and leadership roles.

It’s not that senior developers are struggling to understand DRY. As your experience grows you develop a more nuanced understanding of what it means. The problem I have is that the nuanced understanding contradicts the acronym. It’s more like Sometimes Repeat Yourself.

When DRY backfires

Below is a simple, but real-world example of what DRY can look like in a localisation file:

const submit = 'Submit';

const i18n = {
  submitButtonText: submit,
  submitFormButtonText: `${submit} form`
}

I put this example, because I once inherited—and had to localise—a project with this problem. This is pure DRY. It was like this throughout the codebase. I’d find strings concatenated together in template files as well. The string was repeated, so it was abstracted in to a variable. Unfortunately it has also defeated the point of having the localisation file. Not all languages use the same sentence structure, so this site can no longer be localised. I had to go through undoing all the abstractions. The problem here is that the content is repeated, but the context is different. It was the wrong abstraction.

#software-engineering #programming #technology #software-development #javascript

How to spot the wrong abstraction
1.90 GEEK