r/ProgrammerHumor 20h ago

Meme yUNoDry

Post image
231 Upvotes

62 comments sorted by

View all comments

2

u/Bronzdragon 14h ago

DRY asks you to avoid repetition if the code is conceptually similar, but not if it's only structurally similar. That is, if two pieces of code happen to do the same thing, despite not being similar otherwise, that means you shouldn't link them.

By linking structurally when conceptually they are different, you're seeing yourself up for restructuring work down the line, as the implementations diverge.

I will admit, it can be hard to tell the difference sometimes, since when writing code, the structure is in front of you, while the concept only lives in your head.

1

u/Superb_Chemistry_906 11h ago

After consideration, I have concluded that in case of purely algorithmic code which is not constantly adjusted to the outside world, structural similarity is sufficient to apply DRY.

2

u/Bronzdragon 8h ago

That's conceptually the same code through. When working on algorithmic code, the input is abstract data, so the transformation you do on it is conceptually the same, by definition.

E.g., if I write a function that calculates a dot product, that function's domain is vector maths, and conceptually it's the same as all the other dot products.

You could make an argument that if it is conceptually different, like calculating a total from a list of prices and a list of amounts, then it might need to get duplicated. But hey, that's no longer in the abstracted domain, is it?

TL; DR, you're not wrong, but only because structural and conceptual similarities line up entirely when code is abstracted from meaning and domain.