News for hackers. Stuff that matters.
Re:Don't DRY Your Code Prematurely by Stratoscope on Thursday May 30, 2024 @04:24PM (#40528193)
Sometimes it's best to be DRY right from the start.

Several years ago, I did some contract work for a company that needed importers for airspace data and various other kinds of data relevant to flying.

In the US, the Federal Aviation Administration (FAA) publishes datasets for several kinds of airspace data. Two of them are called "Class Airspace" and "Special Use Airspace".

The guy who wrote the original importers for these treated them as completely separate and unrelated data. He used an internal generic tool to convert the FAA data for each kind of airspace into a format used within the company, and then wrote separate C++ code, thousands of lines of code each.

Thing is, the data for these two kinds of airspace is mostly identical. You could process it all with one common codebase, with separate code for only the 10% of the data that is different between the two formats.

When I asked him about this, he said, "I have this philosophy that says if you only have two similar things, it's best to write separate code for each. Once you get to a third, then you can think about refactoring and making some common code."

That is a good philosophy! I have often followed it myself.

But in this case, it was obvious that the two data formats were mostly the same, and there was never going to be a third kind of almost-identical airspace, only the two. So we had twice the code we needed.

Re:Don't DRY Your Code Prematurely by ebolyen on Thursday May 30, 2024 @04:38PM (#40528380)
I don't know, that sounds like a complex kind of ingest which could be arbitrarily subtle and diverge over time for legal and bureaucratic reasons.

I would kind of appreciate having two formats, since what are the odds they would change together? While there may never be a 3rd format, a DRY importer would imply that the source generating the data is also DRY.

Re:Don't DRY Your Code Prematurely by Stratoscope on Thursday May 30, 2024 @05:10PM (#40528756)
Good point. This may be a case where domain knowledge is helpful.

One of the reasons they brought me in on this project is that besides knowing how to wrangle data, I'm also an experienced pilot. So I had a good intuitive sense of the meaning and purpose of the data.

The part of the data that was identical is the description of the airspace boundaries. Pilots will recognize this as the famous "upside down wedding cake". But it's not just simple circles like a wedding cake. There are all kinds of cutouts and special cases.

Stuff like "From point A, draw an arc to point B with its center at point C. Then track the centerline of the San Seriffe River using the following list of points. Finally, from point D draw a straight line back to point A."

The FAA would be very reluctant to change this, for at least two reasons:

1. Who will provide us the budget to make these changes?

2. Who will take the heat when we break every client of this data?

Re:Don't DRY Your Code Prematurely by ebolyen on Thursday May 30, 2024 @05:44PM (#40529060)
I see, so it's a procedural language that is well understood by those who fly (not just some semi-structured data or ontology). This is a great example of the advantage of domain experience. Thanks for sharing!
Re:Don't DRY Your Code Prematurely by Stratoscope on Thursday May 30, 2024 @06:30PM (#40529469)
> a procedural language that is well understood by those who fly

That is a great way to describe it!

Of course it is all just rows in a CSV file, but yes, it is a set of instructions for how to generate a map.

In fact the pilot's maps were being drawn long before the computer era. Apparently the first FAA sectional chart was published in 1930! So the data format was derived from what must have been human-readable descriptions of what to plot on the map using a compass and straightedge.

I just remembered a quirk of the Australian airspace data. Sometimes they want you to draw a direct line from point F to point G, but there were two different kinds of straight lines. They may ask for a great circle, a straight path on the surface of the Earth. Or a rhumb line, which looks straight on a Mercator projection but is a curved path on the Earth.

You would often have some of each in the very same boundary description!

For anyone curious about this stuff, I recommend a visit to your local municipal airport and stop by the pilot shop to buy a sectional chart of your area.

Re:Don't DRY Your Code Prematurely by tass on Thursday May 30, 2024 @06:56PM (#40529678)
Paper charts are great (they're fairly cheap and printed quite nicely in the USA at least) but you can get a good look at these boundaries through online charts.

https://skyvector.com is a good way to view these.

Re:Don't DRY Your Code Prematurely by Stratoscope on Thursday May 30, 2024 @07:01PM (#40529723)
Thank you! I was trying to remember the name of that site and it slipped my mind. Yes, SkyVector is great.
Re:Don't DRY Your Code Prematurely by hamasho on Thursday May 30, 2024 @10:13PM (#40530802)
I think if you know the domain well, it's not "premature" at all.
Re:Don't DRY Your Code Prematurely by TeMPOraL on Thursday May 30, 2024 @04:47PM (#40528531)
In such case I think I'd go for an internal-DRYing + copy-on-write approach. That is, two identical classes or entry points, one for each format; internally, they'd share all the common code. Over time, if something changes in one format but not the other, that piece of code gets duplicated and then changed, so the other format retains the original code, which it now owns.
Re:Don't DRY Your Code Prematurely by jeremyjh on Thursday May 30, 2024 @06:17PM (#40529343)
This is really good advice and a great way to think about it.
Re:Don't DRY Your Code Prematurely by ebolyen on Thursday May 30, 2024 @04:50PM (#40528568)
I like that approach.
Re:Don't DRY Your Code Prematurely by Spivak on Thursday May 30, 2024 @05:56PM (#40529161)
I've had the mantra "inheritance is only for code reuse" and it's never steered me wrong.
Re:Don't DRY Your Code Prematurely by jameshart on Thursday May 30, 2024 @06:49PM (#40529625)
Inheritance is only good for code reuse, and it’s a trick you only get to use once for each piece of code, so if you use it you need to be absolutely certain that the taxonomy you’re using it to leverage code across is the right one.

All ‘is-a so it gets this code’ models can be trivially modeled as ‘has-a so it gets this code’ patterns, which don’t have that single-use constraint… so the corollary to this rule tends towards ‘never use inheritance’.

Re:Don't DRY Your Code Prematurely by Spivak on Friday May 31, 2024 @12:08AM (#40531395)
Single use? No way that's what multiple inheritance and mixins are for. Inheritance being only for code reuse is explicitly about not creating a taxonomy. No more is-a just, "I need this code here." Hey this thing behaves like a mapping inherit from the MutipleMapping and get all the usual mapping methods for free. Hey this model needs created/updated_at, inherit from ChangeTracking and get those fields and helper methods for free.

Has-a doesn't make sense for code like the literal text reuse. It makes sense for composition and encapsulation.

Edit: I'm now realizing that Python has one of the only sane multiple inheritance implementations. It's no wonder the rest of y'all hate it.

Re:Don't DRY Your Code Prematurely by zeroCalories on Thursday May 30, 2024 @08:31PM (#40530303)
It seems like OP is describing a shared interface, not necessarily inheritance.
Re:Don't DRY Your Code Prematurely by chii on Thursday May 30, 2024 @09:54PM (#40530709)
I believe this very method is very common in games - you have similar logic for entities, but some have divergences that could occur in unknown ways after playtesting or future development.

Tho if done haphazardly by someone inexperienced, you might end up with subtle divergences that might look like they're meant to be copies, and debugging them in the future by another developer (without the history or knowledge) can get hard.

Then someone would wonder why there are these two very similar pieces of code, and mistakenly try to DRY it in the hopes of improving it, causing subtle mistakes to get introduced...

Re:Don't DRY Your Code Prematurely by cies on Friday May 31, 2024 @12:56PM (#40537469)
I prefer the FP approach of separating data and logic. you could end up with a box of functions (logic) that can be reused by the different "entities".

Last time i checked the FP world is slowly producing ECS frameworks that are needed to make the game performant. They used to be nearly C++ (or OO) exclusive.

Re:Don't DRY Your Code Prematurely by trealira on Saturday June 01, 2024 @03:14PM (#40548318)
Is an entity component system really functional programming? I had the sense that functional programming was more about writing functions that are pure and referentially transparent, and making data immutable normally, which can make code simpler and more modular. It tends to use higher-order functions (recursive operators) more than direct recursion, because it's easier to verify correctness then. Rather than imperative loops and mutation, the meat of the program consists of the composition of many different functions, both small and large.

Entity component systems are pretty cool, as is functional programming, but I don't see the relation.

In addition, object-oriented languages seem well-suited to making entity component systems. There are some tutorials on them in different object-oriented programming languages:

C++: https://austinmorlan.com/posts/entity_component_system/

C#: https://matthall.codes/blog/ecs/

Common Lisp: https://edoput.it/2023/11/19/data-oriented-clos.html

Re:Don't DRY Your Code Prematurely by chipdart on Thursday May 30, 2024 @11:46PM (#40531273)
> In such case I think I'd go for an internal-DRYing + copy-on-write approach.

I agree. The primary risk of presented by DRY is tight coupling code which only bears similarities at a surface level. Starting off by explicitly keeping the externa bits separate sounds like a good way to avoid the worst tradeoff.

Nevertheless I still prefer the Write Everything Twice (WET) principle, which means mostly the same thing, but following a clear guideline: postpone all de-duplication efforts until it's either obvious there's shared code (semantics and implementation) in >2 occurrences, and always start by treating separate cases as independent cases.

Re:Don't DRY Your Code Prematurely by bcrosby95 on Thursday May 30, 2024 @05:49PM (#40529088)
I don't know. I've seen this approach for projects before go bad - people didn't want to DRY because they might diverge. Except they never did. Our 3rd+ scenarios we abstracted.

But what basically ended up happening was we had 2 codebases: 1 for that non-DRY version, and then 1 for everything else. The non-DRY version limped along and no one ever wanted to work on it. The ways it did things were never updated. It was rarely improved. It was kinda left to rot.

Re:Don't DRY Your Code Prematurely by jononor on Thursday May 30, 2024 @05:55PM (#40529155)
Why wasn't the original implementation swapped for the new one? The unwillingness/inability to do that seems to be most likely the core of the issues here?
Re:Don't DRY Your Code Prematurely by bcrosby95 on Thursday May 30, 2024 @06:07PM (#40529256)
The majority of our business was through the 1st implementation. Because of that it was the base we used to refactor into a more abstract solution for further scenarios. It was never deemed "worth it" to transition the 2nd non-DRY version. Why refactor an existing implementation if its working well enough and we could expand to new markets instead?
Re:Don't DRY Your Code Prematurely by jononor on Friday May 31, 2024 @03:33AM (#40532378)
Yes, why do it? :p I mean, there are pros and cons - costs and benefits. And I can see both scenarios where it is better to spend the time on something else (that has better chance of bringing in money), and cases where it would be the right thing to do the cleanup (maybe original is just about to fall apart, or the new has straight up benefits to the business, or the act of doing it will greatly improve testing/QA in a critical area, etc).

Writing it DRY in the first place would also have costs, including the alternative costs. Would it have been better to take those there and then?

Re:Don't DRY Your Code Prematurely by chipdart on Thursday May 30, 2024 @11:54PM (#40531324)
> But what basically ended up happening was we had 2 codebases: 1 for that non-DRY version, and then 1 for everything else. The non-DRY version limped along and no one ever wanted to work on it. The ways it did things were never updated. It was rarely improved. It was kinda left to rot.

It sounds to me that you're trying to pin the blame of failing to maintain software on not following DRY, which makes no sense to me.

Advocating against mindlessly following DRY is not the same as advocating for not maintaining your software. Also, DRY does not magically earn you extra maintenance credits. In fact, it sounds to me that the bit of the code you called DRY ended up being easier to maintain because it wasn't forced to pile on abstractions needed to support the non-DRY code. If it was easy, you'd already have done it and you wouldn't be complaining about the special-purpose code you kept separated.

Re:Don't DRY Your Code Prematurely by bcrosby95 on Friday May 31, 2024 @11:13AM (#40536236)
In my experience, once you copy code its bound to diverge, intentional or not. Bugs become features and you can never put the cat back in the bag without a monumental amount of work.

Undoing an abstraction is way easier. Eventually, they all turn bad anyways.

Re:Don't DRY Your Code Prematurely by kiitos on Saturday June 01, 2024 @06:33PM (#40549670)
I have seen far more problems caused by premature or over-use of DRY than the opposite.

Undoing an abstraction is always far more difficult than the pains caused by not having one in the first place.

Re:Don't DRY Your Code Prematurely by mannykannot on Friday May 31, 2024 @09:54AM (#40535205)
I vaguely recall Fred Brooks, in The Mythical Man-Month, using a somewhat similar situation, but involving the various US states' income tax rules and their relationship to the federal tax, as an example in order to make some sort of point (that point being 'know your data', IIRC.)

In a situation where there is a base model with specific modifications - which is, I feel, how airspace regulation mostly works - then I suspect that a DRY approach would make it easier to inspect and test, so long as it stays that way.

Re:Don't DRY Your Code Prematurely by wnevets on Thursday May 30, 2024 @04:32PM (#40528298)
> So we had twice the code we needed.

Was that necessarily a bad thing and something that must be corrected for that code base?

I usually follow the same rule of thumb until I find myself repeatedly updating both at the same time. If I can't update one without updating the other then they must be the same thing and its time to DRY.

Don't Repeat Yourself when updating code.

Re:Don't DRY Your Code Prematurely by Stratoscope on Thursday May 30, 2024 @04:43PM (#40528462)
Good points, thanks for bringing them up.

Yes, there was some ongoing maintenance of this code where both versions had to be updated. The original author was not a pilot and was unfamiliar with some of the nuances of FAA airspace. One of the reasons they brought me in was that I am a pilot and knew how the FAA's data should be interpreted.

In the end, not a huge deal, but it was annoying when I had to make the same changes in two places.

Re:Don't DRY Your Code Prematurely by thdc on Thursday May 30, 2024 @04:58PM (#40528648)
Knowing to DRY there depended on business knowledge that the original author did not have.

While they were wrong in this case, I would say it was a reasonable move to not DRY based on the code pattern itself at the time. And that's the big difference imo - DRYing based strictly on the structure of code vs business processes.

Re:Don't DRY Your Code Prematurely by chii on Thursday May 30, 2024 @09:57PM (#40530718)
But this implies that you have to guess when and where to DRY, which basically implies that there's no good way but via experience and domain knowledge!

That's not what people want to hear - they want a silver bullet; a set of criteria for which DRY'ing could be determined from the onset!

Re:Don't DRY Your Code Prematurely by klyrs on Thursday May 30, 2024 @07:55PM (#40530091)
> Sometimes it's best to be DRY right from the start.

Never zealously adopt a programming practice that can be summarized in a headline!

Re:Don't DRY Your Code Prematurely by OJFord on Thursday May 30, 2024 @08:02PM (#40530147)
Yeah, don't be DRY from the start, YAGNI.
Re:Don't DRY Your Code Prematurely by barfbagginus on Friday May 31, 2024 @02:10PM (#40538474)
I feel like the "sometimes" suggests that in most cases we should zealously not dry things, but in some cases we might want to. Doesn't that make you curious what cases those might be?
Re:Don't DRY Your Code Prematurely by klyrs on Friday May 31, 2024 @02:52PM (#40538924)
> Doesn't that make you curious what cases those might be?

I've been programming for nigh on 40 years; I'm not terribly curious because I've seen successes and failures of pretty much every development paradigm firsthand. But since you ask, my comment above was a self-referential whimsicalism.

I DRY my code in phases. A rule of 3 is decent (e.g., if you've copied a chunk of code three times, give it a think; if you've copied it 9 times, give it a good hard stare).

Re:Don't DRY Your Code Prematurely by steve_adams_86 on Thursday May 30, 2024 @05:44PM (#40529059)
I find this is a case where different pipelines utilizing common functions in different compositions can be a great strategy. If something diverges and a function no longer makes sense in a pipeline, that’s not a big deal. Just pull it out and replace it with something bespoke that does the right thing.

I’ve had a lot of success with this in embedded settings where data is piped into storage or OTA, and I want to format and pack it/send it up consistently but I might want to treat the data itself slightly differently.

Re:Don't DRY Your Code Prematurely by hansvm on Thursday May 30, 2024 @06:53PM (#40529648)
A related concept that IMO still aligns with DRY is that you should only avoid seeming code duplication when things are _semantically_ the same. No matter the mechanism (codegen, generics, macros, inheritance, ...), if you can't give a concept a meaningful [0] name then you usually shouldn't DRY it up with any mechanism. Your example is a technique I also use a lot, but the critical point is that you're choosing to break out functionality which _is_ easy to name.

[0a] More generally, I like a concept of "total" functions -- those which have sensible outputs for all their inputs. It's a bit of a tomayto/tomahto situation defining "all their inputs" (e.g., I'm personally okay using a function name like `unsafe_foo` and expecting a person to read the docs, and on the other extreme some people want sensible answers to anything the type system allows you to input), but the desired end-state is that when the project's requirements change you don't muck around with the ABI and implementation of `count_or_maybe_sort_for_these_three_special_customers_or_else_hit_the_db(...)`, or whatever much more generic and very wrong name the method actually has; the individual components are already correct, so you make the changes at the few methods which are actually wrong given the new requirements.

[0b] Another way of thinking about it is whether the two things should always change in tandem. For two largely overlapping beaucratic data formats? Maybe; there's a comment somewhere in this chain suggesting that they'll never go out of sync, but I'm a bit paranoid of that sort of thing. For the particular data structures that are currently shared by those formats? Absolutely not; if one diverges then you can build the new structure and link it in. The old structure is still valid in its own right.

Re:Don't DRY Your Code Prematurely by magicalhippo on Thursday May 30, 2024 @06:00PM (#40529195)
We've got a large number of customer-specific file integrations, and a lot of them are indeed very similar as the customers have the same system on the other side. However almost all the time there's some tweaking needed. Customer A used field X for this but customer B used the field for that.

So if a new customer comes and need an integration to a system we already support, even if we think they'll start out being identical, we just copy the code.

Thing is, these things evolve. Suddenly we have to patch over some process-related issues in the other system for customer A, while customer B does not have that issue. Now we can fix A's integration without worrying at all about affecting B.

Of course we write library and helper functions, and use those actively throughout, so we only repeat the "top level" stuff.

Re:Don't DRY Your Code Prematurely by PaulStatezny on Thursday May 30, 2024 @08:20PM (#40530244)
> Sometimes it's best to be DRY right from the start.

3 things matter most in real estate: Location, location, location!

3 things matter most in programming: Context, context, context!

DRY – like almost every other programming tool/paradigm/principle – are very often misused by a lack of the programmer's ability to discern correctly whether the tool/paradigm/principle fits in the specific context.

It's not just a science. It's an art, too.

Re:Don't DRY Your Code Prematurely by klibertp on Friday May 31, 2024 @11:57AM (#40536743)
> It's not just a science. It's an art, too.

It was... I will miss that until retirement, too, but the artisan part of this craft has been gradually dying for over a decade. I think this change started when "popular kids" started confidently saying they wanted to work with computers when they grew up. The effect is the proliferation of normies throughout the trade, now many of them with 10+ years of experience. The average developer's appreciation for the elegant and the inspiring grew weaker, and the idea of putting more work into a task than absolutely necessary (like, for example, stopping for a moment to consider the context before deciding on a tool or technique to apply...) lost all appeal. There's a thin line between aggressively pragmatic and ignorant, and the newer generations seem to treat crossing that line as a non-issue as long as the ticket can be presented as resolved. This mindset used to be confined to cubicles and neckties, but now it's seemingly everywhere...

Don't mind me; I just feel unusually old and grumpy today...

Re:Don't DRY Your Code Prematurely by hoosieree on Friday May 31, 2024 @12:12PM (#40536922)
Come check out some of the array languages[1,2,3], or perhaps the retro-computing comfy vibes of a system like Decker[4]. Some of us still appreciate code as poetry.

[1]: https://mlochbaum.github.io/BQN/

[2]: https://www.uiua.org/

[3]: https://code.kx.com/q/learn/tour/

[4]: https://beyondloom.com/decker/tour.html

Re:Don't DRY Your Code Prematurely by klibertp on Friday May 31, 2024 @01:10PM (#40537685)
Oh, I know! While I got discouraged trying to learn K (I was a little too hung up on the notion of free software back in the day...), I learned J and had a terrific time interacting with the community. There were some bad apples, but the basket labeled "Smalltalk" is filled with many marvels. I enjoyed Factor while it was actively developed, which led me deep into Forth-land - an unforgettable experience. By the time I arrived on the other side of s-expressions, Smug Lisp Weenies were out and lots of friendly, curious, intelligent folk lived there instead. More recently, I invested some time into learning Raku - a beautifully eclectic, shockingly expressive language whose development is severely understaffed, underfunded, and underappreciated. I had a great time in all those instances - I know there are passionate people approaching programming creatively in all kinds of shapes and forms, and it's indeed heartening.

The problem is, when I go to work, I see exactly none of those people among my coworkers. I feel like breaking down on the spot and declaiming:

> I've seen things you people wouldn't believe... Attack ships on fire off the shoulder of Orion... I watched C-beams glitter in the dark near the Tannhäuser Gate. All those moments will be lost in time, like tears in rain... Time to die.

I don't, mostly out of respect to Rutger Hauer. Anyway, while in absolute terms people like that are probably much more numerous now than 20 years ago, they feel much more distant to me than ever before.

Re:Don't DRY Your Code Prematurely by barfbagginus on Friday May 31, 2024 @02:09PM (#40538458)
Could you not just fire the people who are not like that and hire the people who are? If not, I think you might not have enough power in the organization! Seek more power!

Also consider finding a job where you need to know category theory to understand the system! That'll give you coworkers who are magical! And a little insane!

Ps. The "C" in "C-beams" now stands for "Category Theory"!

Re:Don't DRY Your Code Prematurely by klibertp on Friday May 31, 2024 @02:28PM (#40538680)
I'm baffled. Which part of my comment triggered you so? You come across as ballistic here. Can't you just "don't mind" my comment? I specifically asked for this, signaling that I don't feel too well right now...
Re:Don't DRY Your Code Prematurely by lost_womble on Friday May 31, 2024 @01:15PM (#40537738)
Yeah, the younger generation must just be wired up differently, it's all those popular kids, can't be anything to do with the proliferation of scrum, agile, and crunch that make them focused on doing all those tickets ;)
Re:Don't DRY Your Code Prematurely by barfbagginus on Friday May 31, 2024 @02:04PM (#40538400)
Trying to educate a boomer (new official term for a millennial) into not complaining about zoomers is like trying to convince a zoomer into not dying inside when they think they have committed an act of cringe.

It's best not to try! These are the cultural delusions that drive us!

Re:Don't DRY Your Code Prematurely by barfbagginus on Friday May 31, 2024 @02:00PM (#40538342)
If you're dealing with those kinds of people, make demands that they begin studying category theory. I'm talking the hard stuff - categories for the working mathematician, toposes triples and theories, sketches of an elephant, McCurdy's 2012 paper on graphical methods for Tanaka reconstruction, and Roman's 2019+ work on coend calculus and diagrammatic optics.

If they do it, if they actually learn categories, then you need to shut the f** up and let them live. They now have the power to do what they want. If they refuse to learn categories then you get to laugh at them and tell them that they are cringe, and fire them.

And if you don't have the power to just fire them for not learning categories, then you very likely need to learn categories yourself! And also gain the power to fire people, lol! There is no rational point to ever complain about a programmer, when you can just fire them instead.

Re:Don't DRY Your Code Prematurely by Stratoscope on Saturday June 01, 2024 @07:21AM (#40544747)
One time I worked with someone who said multiple times, "I closed more tickets in this sprint than anyone else on the team."

What I learned from that is to create a ticket for every slightest little thing.

Correct a typo in a comment? A ticket.

Combine a tiny bit of duplicate code? A ticket.

Add a little error check? A ticket.

Forget working together to create a great and well-supported product.

What counts is whether I closed more tickets than you!

Re:Don't DRY Your Code Prematurely by hakunin on Thursday May 30, 2024 @09:12PM (#40530506)
Did the repetition become obvious before or after you saw both implementations? It’s possible that if you dried it up right away, you would’ve abstracted the wrong thing, and it’s way more obvious only in hindsight.
Re:Don't DRY Your Code Prematurely by Stratoscope on Thursday May 30, 2024 @10:52PM (#40531038)
This was some years ago, so my memory may be foggy - and I'm not instrument rated!

As I recall, it was after I saw the two implementations. I got curious and looked at the original FAA data and specifications and saw how much the two kinds of airspace have in common.

Re:Don't DRY Your Code Prematurely by barfbagginus on Friday May 31, 2024 @01:51PM (#40538214)
How was the dried code to own and maintain before and after? Was there pain before, and did it go away? Or did the operation prove to be more of a nitpick/bike shed? How long did you stick around with eyes on the code?
Re:Don't DRY Your Code Prematurely by Stratoscope on Saturday June 01, 2024 @06:51AM (#40544568)
Interesting questions, thanks for asking!

We never got that far. At that point, the code was what it was. Despite any possible design flaws, it worked. And dealing with FAA data that really never changes at all, once it worked, that was good enough.

"If it ain't broke..." ;-)

My job after that was to move on to other countries' airspace and obstacle data. So I tried to apply the appropriate amount of humidity on those.

Re:Don't DRY Your Code Prematurely by BurningFrog on Thursday May 30, 2024 @10:37PM (#40530939)
I'm a fan of the "Rule of Three", but another great rule is to not mindlessly follow rules.
Re:Don't DRY Your Code Prematurely by heleninboodler on Friday May 31, 2024 @11:43AM (#40536598)
Call me crazy, but it's almost as if these should be guidelines considered by a thinking person, with experience to help inform them, rather than hard-and-fast rules that must be applied to everything.
Re:Don't DRY Your Code Prematurely by cjfd on Friday May 31, 2024 @03:25AM (#40532333)
This happens when people follow rules mindlessly. 3 is an arbitrary number anyway. Even if 3 is the right number in most cases there will be cases where abstracting after 2 cases is best and others where abstracting after 4 cases is best, or any other number, really.
Re:Don't DRY Your Code Prematurely by jvans on Friday May 31, 2024 @06:56AM (#40533464)
This is just being hindsight oriented. The way it worked out it was better to DRY from the start, but the person implementing it didn't know how the chips would fall.

If you do what this guy did and are wrong, it looks like yes obviously it was the same thing and you should have DRY right from the start.

If you DRY from the start and are wrong, someone writes a blog post about not DRYing from the start.

It's a bet on which mistake is worse, because you will be wrong sometimes. DRYing from the start IMO is a worse mistake than duplication.

Re:Don't DRY Your Code Prematurely by Stratoscope on Saturday June 01, 2024 @07:36AM (#40544857)
That is a great insight.

As always, "You don't know what you don't know."

One time the guy who wrote the original code asked, "Why do they have towers at airports? Aren't the controllers down underground looking at radar screens?"

It took two of us who were pilots to convince him that yes, the tower is up there so the controllers can see the airplanes with their own eyes. And yes, they are provided with binoculars.

We all form mental models of the world around us. The best we can hope for is to update those models as we receive new information.

Re:Don't DRY Your Code Prematurely by tossandthrow on Friday May 31, 2024 @07:21AM (#40533644)
It is easy to come in after the fact and say this.

In reality: Had he DRYed it up from the beginning, you would probably have complained about a codebase that needs to corner cases into account deep in the code - the story had been turned around.

Yes, it is annoying to get into an existing code base.

Re:Don't DRY Your Code Prematurely by baja_blast on Friday May 31, 2024 @12:03PM (#40536809)
But you're refactoring an existing codebase so it's not prematurely dry. You know exactly how these classes diverge.
Re:Don't DRY Your Code Prematurely by gloryjulio on Friday May 31, 2024 @12:18PM (#40536995)
> Sometimes it's best to be DRY right from the start.

In your case I don't see how it couldn't be refactored later on once you have the domain knowledge. In our company we always have 2 types of projects planned. We can't always ship new features under type 1. And refactoring is something we must do under the 2nd type to reduce the tech debt. With 2 type of projects in mind we always plan the capacities for both types of projects

And that's how exactly how you should do dry. Once you see the repeated pattern, you should refactor it.

I'm in the finance factor and we are seeing this kind of 'similar but not the same' problem a lot. We are constantly in the process onboarding new payment use cases while doing refactors to abstract away the common patterns.

Re:Don't DRY Your Code Prematurely by barfbagginus on Friday May 31, 2024 @02:22PM (#40538619)
In some cases I would prefer to have two separate clear yet repetitive use cases, than to have, for example, a single abstract use case, that gets injected with two different factories at configuration time, depending on which sub case you want.

In that case, reading and maintaining two simple use cases might be less work than reading an abstract use case, backtracking to the available factories, and then mentally interpreting the injection and factory behavior.

Unless your abstractions really really make things just way simpler, being explicit could be better.

Another place where this repetition tends to help readability more than it hurts maintainability is in test cases. Often abstracting things out with a little test fixture is helpful. But then being obsessive about this ends up making tests harder to maintain since there's all this long distance coupling that you constantly have to maintain.

It seems that outside of test cases and use cases, we want to be much more diligent about DRY and picking the right abstractions - that makes logic in our use cases much simpler and more coherent. While inside the use case or test case, a little duplication of business logic is not so bad, and can actually improve the narrative of the code.

Re:Don't DRY Your Code Prematurely by eddd-ddde on Friday May 31, 2024 @11:02PM (#40542541)
That sounds like it was as easy as copy paste.

The day one of those formats changes slightly, it's really easy to update it. If you had a single code base, you now have to think how to refactor out the differences. In my experience when those differences appear is more likely to not be trivial.