this post was submitted on 11 Jun 2023
6 points (80.0% liked)

Programming

3347 readers
1 users here now

All things programming and coding related. Subcommunity of Technology.


This community's icon was made by Aaron Schneider, under the CC-BY-NC-SA 4.0 license.

founded 2 years ago
MODERATORS
top 12 comments
sorted by: hot top controversial new old
[–] [email protected] 9 points 1 year ago

DRY is nice and all, but never let your code get so DRY it chafes.

[–] [email protected] 5 points 1 year ago

I think it’s one of those things that is generally a good instinct, but easy to go overboard and way over index on.

Usually I try to optimize code for “how easy is it to entirely delete this module” - loose coupling is the bigger concern. Sometimes it makes sense to repeat code with this approach. Usually I’ll abstract a shared concern out once I repeat it 3 or so times, but not always.

It’s all about trade offs.

[–] [email protected] 4 points 1 year ago (1 children)

I often see this problem in the testing world, particularly around frontend tests that utilize UI automation tools.

The pattern I see is often to abstract chunks of common steps into individual functions that often live in places very disconnected from the test. While this might reduce the number of lines of code in a test and arguably make it more maintainable it has its problems.

Main problem number one is that readability has been diminished. It is now harder to understand exactly what this test is doing because steps have been abstracted away. Tests that can be clearly understood, read and describe functionality and behaviors are immensely important to getting others to quickly understand code. I hate to put a barrier there to making that happen.

Second, i don't truly believe it ALWAYS improves maintainability. This decision of abstracting carries a risk. When that abstraction needs to change in one place you are faced with a tough choice...

Does this need to change in ALL places? How do you know? How can you get all places it is used and be certain it has to change in all of them? Changing for all usages is RISKY particularly when there are large numbers of uses and you don't know what they all do.

Do i make a new abstraction? This is safer but now starts to create bloat. It will lead down paths of making future implementations trickier because there are now two things to choose from that are possibly slightly different.

For tests I'm not really convinced that these problems are worth dealing with. Keep it simple and understandable. Repeating yourself for the sake of clarity is okay. I'll say it again... Repeating yourself for the sake of clarity is okay!

[–] hackeryarn 1 points 1 year ago

Yeah, I've seen this a ton. And it sucks because it's always done with good intentions.

That's why I think that if you need small helpers keep them short and prevent them from going too deep. Nothing is worse than digging through 10 layers of functions just to figure out what's going on.

[–] [email protected] 2 points 1 year ago* (last edited 1 year ago) (1 children)

The example is more of an issue with using the right tool for the job: using classes inheritance and overloading would properly deal with the employee/manager/c-suite distinction.

[–] hackeryarn 1 points 1 year ago

For sure there are better abstractions that would help. I still think that they only help with over abstraction to a certain point. Digging 5 classes deep just to figure out what's actually happening is just as, if not more, frustrating than digging 5 functions deep.

[–] [email protected] 1 points 1 year ago* (last edited 1 year ago) (1 children)

This just serves as another reminder that I need to finish reading SICP. But that said, I think this brings up some very interesting points. The example provided of DRY is focused on what is happening on a human/company level, while the abstraction barriers provided focus heavily on what is happening on a software level. This is a differentiation that I feel like is extremely important when programming robust, maintainable software. You cannot let non-software related terminology seep into what is fundamentally, well, software.

When you let non-software terminology work its way into software, the software has to start making assumptions. What is a C level employee? What bonuses do they require? Are these things subject to change? The list goes on. But if you approach the problem with software first and foremost, it is clear that all is happening is a variable bonus is added to an employees compensation. It is not this layers problem what that value is, nor is it this layers problem who is being compensated. That is all concerns for a DB layer (of some form) somewhere up the chain. All the financial layer cares about is applying the calculations.

So I don't feel like this is really a case against DRY, as much as it's a case against using non-software terminology and applying non-software assumptions to what is fundamentally, software. The arguments for maintaining independent layers is also important, but if you're thinking fully in terms of software operation, I feel you can more comfortably determine when layers can be interlinked.

[–] hackeryarn 1 points 1 year ago (1 children)

Yeah, this definitely went deeper. I was really tempted to change the title to be honest. The article started out of a frustration of a code base that was too DRY, and kind of grew into a more general how to create abstractions. The original title just stuck around.

[–] [email protected] 2 points 1 year ago

That absolutely makes sense, still thoroughly enjoyed the article!

Something else interesting to think on, is which terminology helps the average programmer more. If DRY causes habits to form around human/business terminology maybe it should be avoided in favor of abstraction layers! No idea what the answer is in this regard, fully possible theres no difference. But its an interesting thought experiment all the same.

[–] [email protected] 1 points 1 year ago

DRY is necessary to apply but not solely through abstraction

load more comments
view more: next ›