logoalt Hacker News

hansvm • today at 2:24 PM • 0 replies • view on HN

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.