11 Comments

Would using static type help? Like if you have a configuration as a type that can remind you whenever you missed a new key?

Expand full comment

I believe it will, but I don't know how to enforce it well, as the returned values are to be consumed by another function that expects `Record<string, string>`.

I could introduce a type of `interface EnvVar extends Record<string, string>`, but this means the issue still happens unless the dev has the habit of updating the type first.

Expand full comment

Love the story! You could also explain your preferred style based on coupling. Removing duplication can reduce coupling.

Expand full comment

Does de-duplication reduce coupling, though? At best it replaces one kind of coupling with an other, more manageable one. Worst case it increases the coupling because you can no longer change the two places independently.

Expand full comment

Sometimes. Take the example of magic numbers -> constant. If we have a "3" in the code in 2 places but meaning the same thing ("this is a suspended account" for example), then introducing "SUSPENDED_ACCOUNT = 3" & using that instead of the constant 3 reduces coupling. If we need to change the number, we change it in one place.

However, what if 3 means "suspended account" in one place and "valid zipcode" (or something else completed unrelated) in another. Introducing "THREE = 3" & using it in both places now *creates* coupling (please don't laugh, I've seen it repeatedly).

Extend this example to expressions, statements, functions, and objects. Saying the same thing in 2 places is coupling.

Expand full comment

I think "3" is a great example here. In some cases it's just not so obvious when we trying to "simplify" or "dedup" some similar logic, but they are not referring to the same thing - only on the surface they looks alike.

Expand full comment

That's why design always requires judgement. Two things can look similar but mean different things, or similar & they can be made identical, or quite different but actually compute the same thing.

Expand full comment

It looks like THREE in this scenario is about how broadly reusable the name is.

So, the more open a name is for interpretation, the more likely it is to introduce unintentional coupling 🤔

Expand full comment

It also nicely circles back to cognitive function. 3 = THREE is not something I am likely to forget.

Expand full comment

Thank you! I need to use 'coupling' more in fast conversation as I couldn't make it effective (I can't articulate it succinctly).

But I also just realised that I can link coupling back into "forgetting": "That's coupling". "So?". "Coupling implies a chance to forget changing the other element."

To make it more succinct, "Coupling exacerbates inattentional blindness".

Expand full comment

One way to identify coupling is to look for "remember" in comments: "If you change this, remember to also change that".

Expand full comment