Is it dry?

DRY doesn't necissarily mean less verbose.

DRY. Don’t Repeat Yourself.

Frequently misunderstood.

Today I did some DRY that might confuse a few people.

I ran across a comment in a .go file that said “Keep this in sync with the frontend code”. And do you know what’s worse than having two bits of code tied together by comments? Two bits of code held together by a single comment. Yeah. That’s right

Eww.

I hate these types of comments. This code is so fragile. These bits of code easily fall out of sync.

And this is precislye what DRY is meant to guard against. We want a single point of truth. We don’t want knowledge duplicated in multiple places throughout our code, where they might fall out of sync.

So what was my solution?

I wrote a short JavaScript script that would generate a short .go file, replicating the JavaScript code with a Go equivalent.

All in all, what was 2 lines of code (one in a JS file, one in a Go file) plus a comment, became approximately a 5-line JS script, which generated a 5-line Go file. Final score: 10 new lines of code, to replace 1.

But now the code is now DRYer. And more verbose at the same time.

Share this