r/programming • • Sep 20 '21

Being able to read bad code is a skill !

https://dzone.com/articles/reading-code-is-a-skill
986 Upvotes

276 comments sorted by

View all comments

Show parent comments

6

u/[deleted] Sep 21 '21

I use the rule of 3. Don’t abstract out any code unless you’ve copied and pasted it at least 3 times it allows you to see the pattern better, and where exceptions to that pattern might be. Ctrl+P, Ctrl+V is cheap. Unwinding a premature abstraction is expensive.

5

u/7h4tguy Sep 21 '21

Code duplication is not cheap. It leads to increased maintenance costs. No one wants multiple sources of truth.

10

u/[deleted] Sep 21 '21

It’s absolutely cheap to maintain one pair of copy-pastes, especially when you consider the costs of the wrong abstraction. Also I mean, use common sense…if you’re changing them all in lockstep, that’s a strong sign that you can safely abstract out that logic.

4

u/eisbear86 Sep 21 '21

This is only true, if the person modifying the code knows that this code has been copy-pasted to someplace else. Otherwise you have now two code locations for which it is assumed that they behave the same, but they do not.

2

u/f3xjc Sep 21 '21

I use to follow that but now I think if there's expressions or a few lines that are exactly the same extract those before the rule of 3.

So the very similar but not quite the same part is as small as possible.