logoalt Hacker News

pmg101 • today at 7:50 AM • 2 replies • view on HN

Sometimes syntactic duplication can give you a hint to underlying semantic duplication. But it's no more than a hint.

DRY doesn't mean don't repeat a similar looking block of code, it means try to structure your code so that each element of the design has a single representation.

There's no shortcut to good design where no thinking is required, much as a lot of people seem to wish there were.


Replies

hansvm • today at 2:24 PM

Absolutely. The rule of thumb is that two things should be abstracted together iff they should change together. The down-sampling constant matching the upweighting factor? Don't repeat yourself. Needing a half-dozen subtly different circular buffer definitions? Circumstances vary, but repetition is probably fine. Two pieces of code which are literally identical? As you said, the syntactic duplication suggests there's an underlying semantic duplication you might want to iron out, but if the two callsites really want the potential for independent behavior then byte-for-byte code duplication can make more sense.

But, like the rest of software, even that's just a guideline. Suppose you've been painted into a corner where code needs to be synced between languages. There are solutions, but especially if it happens rarely then those solutions might be a lot more expensive than just a comment at both locations.

N_Lens • today at 9:24 AM

What do you mean, Claude just told me my architectural ideas were terrific and we're halfway through implementing a restructure of the company's codebase!

➕ show 1 reply