News for hackers. Stuff that matters.
Re:Don't write clean code, write CRISP code by dkdbejwi383 on Tuesday April 18, 2023 @06:27AM (#35612136)
DRY is probably my least favourite programming meme. There are far too many overzealous juniors who learned it and have a bee in their bonnet about creating absurd abstractions around any two lines of code (or config) that have vague or imagined similarities, locking in all sorts of annoying indirection.
Re:Don't write clean code, write CRISP code by m_mueller on Tuesday April 18, 2023 @06:40AM (#35612210)
Totally agreed. I've tried explain exactly this issue in a talk [1], trying to find a useful synthesis between different paradigms like KISS, DRY and YAGNI.

[1] https://www.youtube.com/watch?v=0FIZn2trkoA&pp=ygUXbWljaGVsI...

Re:Don't write clean code, write CRISP code by gonzo41 on Tuesday April 18, 2023 @06:56AM (#35612309)
I asked everyone's favorite chat AI to come up with a new buzzword using the letters from those.

The best was KINDergartener (uses all the letters). I would describe this as a programming paradigm for people who are new to the programming and real work and havn't found out that the getting shit done and shipping the code to users is the most important state for code to be in.

Re:Don't write clean code, write CRISP code by dathinab on Tuesday April 18, 2023 @07:50AM (#35612721)
The trick is to understand this paradigms, why they came to be and what they want to archive and as much important when to not apply them.

Then program with the things you have learned in mind, instead of blindly following the some "paradigms rules", 'cause that's never ending well.

Re:Don't write clean code, write CRISP code by Symmetry on Tuesday April 18, 2023 @10:09AM (#35614268)
I made a DRY/YAGNI yin/yang sticker for my laptop a little while ago.

https://i.redd.it/16qm2a3m7m991.jpg

Re:Don't write clean code, write CRISP code by physicles on Tuesday April 18, 2023 @06:38AM (#35612193)
For best results, set your DRY dial to 8 or 9 out of 10. Settings higher than 9 result in premature or excessive abstraction.

But if you hate DRY so much, imagine a word where people set it to 1 or 2. I wouldn’t want to work in that code base.

Re:Don't write clean code, write CRISP code by swader999 on Tuesday April 18, 2023 @07:05AM (#35612372)
Whatever you do, never turn it up to 11,
Re:Don't write clean code, write CRISP code by zambal on Tuesday April 18, 2023 @07:12AM (#35612429)
But what if you just need that extra bit of dryness and you're already at 10?
Re:Don't write clean code, write CRISP code by fendy3002 on Tuesday April 18, 2023 @07:52AM (#35612735)
heck, sometimes even 7 is better than 9
Re:Don't write clean code, write CRISP code by postalrat on Tuesday April 18, 2023 @08:58AM (#35613391)
Often the best way to DRY something if to let it sit in the sun a while where everyone can see it.
Re:Don't write clean code, write CRISP code by IshKebab on Tuesday April 18, 2023 @06:55AM (#35612295)
I put it second behind "premature optimisation" which is used to justify never caring about performance at all (until it's too late and you can't do anything about it).
Re:Don't write clean code, write CRISP code by DarkWiiPlayer on Tuesday April 18, 2023 @08:37AM (#35613162)
Actual premature optimisation is harmful for the same reason as premature abstraction. In both cases, improvements that come "for free" should be done without thinking, but one they start to impact readability, we should consider how beneficial those changes really are.
Re:Don't write clean code, write CRISP code by marcosdumay on Tuesday April 18, 2023 @10:29AM (#35614489)
If you don't know what you are building, any action is premature.
Re:Don't write clean code, write CRISP code by laerus on Tuesday April 18, 2023 @06:55AM (#35612296)
DRY only paired with the rule of three (or more)
Re:Don't write clean code, write CRISP code by carlmr on Tuesday April 18, 2023 @07:07AM (#35612393)
That, or the code block is sufficiently big and similar.

I remember a ex-colleague of mine who copied a 1000 line function, changed a parameter, and didn't see anything wrong with it.

Re:Don't write clean code, write CRISP code by leidenfrost on Tuesday April 18, 2023 @07:25AM (#35612513)
Why do you have a 1000 line function in the first place? Sounds like an entire class of methods in a single structure.
Re:Don't write clean code, write CRISP code by tcfhgj on Tuesday April 18, 2023 @08:40AM (#35613207)
The function contained 300 lines which he copied and pasted twice, because DRY and extracting Tonto a function is dumb /s
Re:Don't write clean code, write CRISP code by carlmr on Tuesday April 18, 2023 @08:51AM (#35613319)
Created by the same person. But the way he doubled his LoC contribution in this PR you could imagine why.
Re:Don't write clean code, write CRISP code by sodapopcan on Tuesday April 18, 2023 @10:45AM (#35614701)
I had a coworker who did similar. It was essentially the entire frontend for an order application (back in the jQuery days), a few thousand lines. That was all copy/pasted, changed to deal with the case of when there are no orders (which only ever happens once since this was an in-house system) and those massive chunks were wrapped in an if/else.
Re:Don't write clean code, write CRISP code by bcrosby95 on Tuesday April 18, 2023 @11:18AM (#35615131)
I recently had a junior copy paste a 20 line code block rather than add an extra 'or' to the if statement.

Hey, rule of 3 ;)

Anyways, guidelines are just guidelines, regardless of what you come up with you can break it.

Re:Don't write clean code, write CRISP code by erik_seaberg on Tuesday April 18, 2023 @03:09PM (#35618545)
I’m not swayed by this rule of three. The experience I have is the reason I reach for a hammer immediately, instead of first trying three rocks to demonstrate that’s a bad idea.
Re:Don't write clean code, write CRISP code by leidenfrost on Tuesday April 18, 2023 @07:29AM (#35612548)
The worst part of DRY is that developers worrying about the amount of characters in a source file rather than readability, as if the cimpiler had a hard time reading thos eextra few kbs.
Re:Don't write clean code, write CRISP code by orbz on Tuesday April 18, 2023 @08:02AM (#35612829)
But remember you have to get WET (write everything thrice) before you get DRY.

I apologize for my dry humor.

Re:Don't write clean code, write CRISP code by el_oni on Tuesday April 18, 2023 @08:08AM (#35612884)
True, but then you also get times when somebody who should know better implements a "in current financial year w/ special cases" function 3 times.

Then your tester says it isn't correct in 2/3 places, you find out you've only updated the function in 1 place and either abstract those the other 2 cases out or update it in those 2 places aswell.

Re:Don't write clean code, write CRISP code by _0ffh on Tuesday April 18, 2023 @08:30AM (#35613099)
Seems whenever someone popularizes a rule of thumb, expressed in a catchy way, some newbies will take it as inviolable absolute law that everything else must be bent around, at any cost.
Re:Don't write clean code, write CRISP code by andrewingram on Tuesday April 18, 2023 @08:33AM (#35613130)
My take is that all this WET/DRY wisdom makes people think about abstractions in the wrong way -- it makes us debate them in term of when, rather than what and why.

In my mind, the point of abstraction is to transform models such that we can build solutions in a way that is a better fit for the problems at hand. Reducing LOC and repetition is explicitly not the goal, sometimes a good abstraction may actually result in more lines of code (but more often it's less). So reducing LOC and repetition is commonly a happy byproduct, rather the reason we do it in the first place.

I see slightly more experienced programmers rebound from DRY to a point of having a pathological distaste for abstractions, and I find those codebases to be far more stressful to work in than ones which just happen to have a few bad abstractions.

Re:Don't write clean code, write CRISP code by zelphirkalt on Tuesday April 18, 2023 @11:24AM (#35615218)
I agree with rather having good or correct abstractions than saving LOC. However, a bad abstraction can give readers/devs the wrong idea about what is behind it, how it works or how it can be used and when to rely on it. Unreliable abstractions are terrible. Leaky abstractions are also terrible. Both introduce a lot of mental load overhead.
Re:Don't write clean code, write CRISP code by erik_seaberg on Tuesday April 18, 2023 @03:03PM (#35618451)
Some people choose “no abstraction” over “poor abstraction,” which I don’t understand. Working without any abstraction is the same as working with an infinitely leaky abstraction.
Re:Don't write clean code, write CRISP code by andrewingram on Wednesday April 19, 2023 @06:48AM (#35626597)
Yeah. A pervasive poor abstraction can be painful, but in my own experience most misguided abstractions tend to be somewhat superficial and easy to unwind. It does worry me to see so much rhetoric in the wider programming community encouraging people to avoid abstractions, because this is the kind of lesson that is easy to imprint on newcomers -- but I suspect the easiest way to get good at them, is to do them badly a few times.
Re:Don't write clean code, write CRISP code by tcfhgj on Tuesday April 18, 2023 @08:37AM (#35613169)
For me DRY is one of the most important principles for maintainable software. Imagine having to fix a bug at two independent places or having to add the same conceptual functionality at two different places. Totally unnecessary bugs guaranteed, and ready to be thrown away after some time, because a change will be harder than a rewrite.

It's enough pain when the effort is too big to avoid (e.g. Js frontend, Rust Backend) it.

Juniors will learn the difference between actual and imagined similarities at some time.

Re:Don't write clean code, write CRISP code by glanzwulf on Tuesday April 18, 2023 @09:35AM (#35613801)
Funny enough this happened to me yesterday. I had to do a small change of code in a component and then when I pushed the code I got an error back. After searching for what was happening, it was actually a code I copied and named "Component2" two years ago when I was starting out. Today I would not have made it the way I did, but in my first month...

And thank god for github actions! :D

Re:Don't write clean code, write CRISP code by orthoxerox on Tuesday April 18, 2023 @09:58AM (#35614125)
Premature dryness is the real issue, not the principle itself. When you catch yourself fixing the same code in multiple methods again, it's a good time to stop and refactor.
Re:Don't write clean code, write CRISP code by senbrow on Tuesday April 18, 2023 @10:44AM (#35614683)
DRY should really be "DRA" - don't repeat abstractions (which admittedly isn't as catchy).

The problem with DRY is when abstractions with currently identical implementations are given a single interface, even though they're logically distinct.

Then, when those distinct abstractions' implementations need to diverge, you've got a rats nest of references to manually pick through and separate out.

Or worse yet , the mistakenly-shared interface becomes parameterized, leading to a horrible mixing of requirements and concepts that may never be untangled if the original intent is lost to time.

Obviously DRY is great when you can consolidate multiple implementations that are actually a single abstraction, but it can really go off the rails if the motivation for the mantra isn't understood.

Re:Don't write clean code, write CRISP code by throw1234651234 on Tuesday April 18, 2023 @08:59AM (#35613396)
DRY is priceless, and if your argument is to avoid DRY in very specific cases, state that specifically.

DRY, when used with functional, well named code is the number one thing keeping a codebase easy to read. There should be one way to achieve something well-defined like save/edit/delete an entity, check a file type, url encode a string, etc.

This ALWAYS leads to much easier code fixes AND much easier refactoring.

Re:Don't write clean code, write CRISP code by lolinder on Tuesday April 18, 2023 @09:47AM (#35613958)
Yes, that happens, but I've also had to take over a large iOS app written by a self-taught programmer who never learned DRY. They didn't factor out any shared code whatsoever. Any time they needed to do something that had already been done, they just bounced over to the other controller and copied the code over.

Of course, the copied code was often buggy, and they would only fix the instance in the controller where the bug was reported. When bugs were fixed it was pretty much random chance which version would be chosen to copy when needed again, so creating the right abstractions involved piecing together an entire phylogenetic tree of the code to decide which pieces were supposed to be the same and which were legitimately different.

Add a commit history that looked like "v1", "v2", "v3", and... yeah. We finally decided it would be cheaper to scrap the app and rewrite it from scratch.

So, yes, premature abstraction is a huge problem, but so is the opposite extreme, and after that experience I personally would choose overly-indirect code over the mess I inherited. It's easier to untangle a function that has too many callers than it is to find code that should have been shared after months of divergent evolution.

Re:Don't write clean code, write CRISP code by numbsafari on Tuesday April 18, 2023 @10:55AM (#35614811)
When I was younger, I really thought this was the be all end all, and probably committed more programmatic sins due to trying to achieve DRYness than anything else.

Often times it seems like we aim for DRY at the expense of simple, or idiomatic code. It also has a nasty habit of making code difficult to change since it leads to a lot of premature coupling, where someone sees a repetition and naively assumes that that needs to be eliminated, when it might literally be a naturally independent value.

Re:Don't write clean code, write CRISP code by raffraffraff on Tuesday April 18, 2023 @12:27PM (#35616184)
It's definitely something that people overdo. But sometimes, especially with things like terraform, you have to struggle against the DSL to avoid having thousands of lines of repetitive code to do what could done in 100. I'll go quite fast to avoid the situation I saw at at previous company, where they had 36k lines of trash to run a few data centers. Deploying a new data center was next to impossible.
Re:Don't write clean code, write CRISP code by bazoom42 on Tuesday April 18, 2023 @12:32PM (#35616269)
My take is the opposite. DRY can be overused, but it is much easier to “undry”, than it is to consolidate code which should have been dry from the beginning.