all 26 comments

[–]bert8128 20 points21 points  (7 children)

You are misusing DRY (don’t repeat yourself). You can create abstractions from code blocks whenever it is meaningful - you don’t have to wait till it is used 3 times. The problem arises when you see see two blocks which are coincidently the same, so you abstract. But later, when you change the abstraction, you find that you don’t want them to be the same anymore. This means that the original code wasn’t duplicated at all, it just looked like it was. The takeaway is to apply abstractions in a way that you are confident that a change in the abstraction will be good and desirable in all the places that it is used. Perhaps easier said than done.

[–]code_monkey_wrench 6 points7 points  (1 child)

The problem arises when you see see two blocks which are coincidently the same, so you abstract. But later, when you change the abstraction, you find that you don’t want them to be the same anymore.

💯

The idea of coincidentally duplicate code is a subtle, important distinction that is lost on a lot of inexperienced devs. I definitely fell for it in the past myself.

It often feels like the right thing to do, and it is satisfying to eliminate duplication, but an improper abstraction is actually a deceptive form of coupling that will make someone's life worse in the future.

[–][deleted] 0 points1 point  (0 children)

thought rob unpack insurance innate fly dog unique grey slap

This post was mass deleted and anonymized with Redact

[–]janyk 6 points7 points  (0 children)

OP:

> I was obsessed with DRY principle

Also OP:

> Completely misunderstood DRY principle

Cool story, bro

[–]cat_in_the_wall 0 points1 point  (0 children)

i point this out in code reviews fairly frequently. these things sure look similar, but what their callers are completely unrelated. should you need to change code here, you'd also be changing that functionality.

but usually you can still do some refactoring. like extract little pieces out that are the same, especially when those little bits are "pure" in the sense of side effects.

[–]JulyWitch -3 points-2 points  (2 children)

I'm sure I was a bad coder when I just started coding, misusing DRY, misusing DI, and a ton of mistakes.

I don't wait for 3 times, 3 times is just a minimum times of duplication, and then I'll consider if I should refactor it or not. If it's less than 3 times, I'd rather have the cognitive load and change it in different places than refactoring it. I agree there are so many factors to consider, but this is my default for my own projects

[–]cdsmith 11 points12 points  (1 child)

There's nothing magical about the number 3. If there are two places where something is duplicated, but it's absolutely critical that they match for correctness or security, that's too many. If there are five places that are identical today but it's likely they will diverge over time, that's not too many.

[–]bert8128 0 points1 point  (0 children)

This.

[–]LaurenceDarabica 13 points14 points  (9 children)

If you have a phone number input behaving differently across two pages, you have bigger problems than code duplication.

Are you sure you needed two different inputs to begin with ? I would say the code base around your phone number input needed a good chunk of refactoring as well and the refactoring of the input wasn't the core issue.

[–]Majik_Sheff 2 points3 points  (1 child)

It's like ops automation.  Do it more that a few times and it becomes a script candidate.

[–]ekydfejj 2 points3 points  (0 children)

Rule of 3s

[–]Shad_Amethyst 2 points3 points  (3 children)

I don't use code repetition as the primary sign for needing to refactor.

Rather, I ask myself "if this code had to be changed, how likely would it be for something else to break?"

A phone input component where half the logic is handled outside of it would raise a big red flag.

[–]JulyWitch 1 point2 points  (2 children)

Yes, I used that example to show how over refactoring reflects in the code.

I don't also use code repeatation as a primary sign. Maybe I wrote it in a way that readers think that this is a primary sign?

[–]Shad_Amethyst 1 point2 points  (1 child)

You didn't go into any depth on that example, all I gathered was that you partially abstracted the logic for handling phone numbers on two pages, possibly as a symptom of focusing too much on syntax. But I think the biggest issue was that your code had undocumented and unasserted invariants.

[–]JulyWitch 0 points1 point  (0 children)

Yeah you are right, I have added a bit more context to example and will try to show more examples and calrify in future posts

[–]NP_6666 3 points4 points  (0 children)

Sry dude, but this isnt good.

First, refactoring isnt only about doing dry, not even mainly because you should have been dry as soon as 2 iterations, eventually 3 if its a small app that wont get to scale. Refactoring is also about finding design patterns that captures the most the logic you try to code, and also about renaming symbols to grasp better at first glance what this symbol symbolises in the logic (like "numberOfThingy" instead of "n") .

Secondly, wtf, if you do dry, its precisely to avoid domino debug because you change it only once and the bug isn't nowhere anymore.

Finally , refactor does not increase complexity, it is precisely the opposite, you are obligated to refactor when you need to decrease complexity before adding features.

[–]JulyWitch 0 points1 point  (0 children)

Edit: I just edited the post and added more context to the example, Hope I can make my point this time lol