News for hackers. Stuff that matters.
Re:It's probably time to stop recommending Clean Code (2020) by anonyfox on Tuesday May 25, 2021 @10:30AM (#27277249)
> WET (Write everything twice), figure out the abstraction after you need something a 3rd time

so much this. it is _much_ easier to refactor copy pasta code, than to entangle a mess of "clean code abstractions" for things that isn't even needed _once_. Premature Abstraction is the biggest problem in my eyes.

Write Code. Mostly functions. Not too much.

Re:It's probably time to stop recommending Clean Code (2020) by allo37 on Tuesday May 25, 2021 @10:43AM (#27277428)
I think where DRY trips people up is when you have what I call "incidental repetition". Basically, two bits of logic seem to do exactly the same thing, but the contexts are slightly different. So you make a nice abstraction that works well until you need to modify the functionality in one context and not the other...
Re:It's probably time to stop recommending Clean Code (2020) by fiddlerwoaroof on Tuesday May 25, 2021 @11:11AM (#27277854)
If you mostly deduplicate by writing functions, fixing this problem is never very hard: duplicate the function, rename it and change the call-site.

The interesting thing about DRY is that opinions about it seem to depend on what project you’ve worked on most recently: I inherited a codebase written by people skeptical of DRY, and we had a lot of bugs that resulted from “essential duplication”. Other people inherit code written by “architecture astronauts”, and assume that the grass is greener on the WET side of the fence.

Personally, having been in both situations, I’d almost always prefer to untangle a bad abstraction rather than maintain a WET codebase.

Re:It's probably time to stop recommending Clean Code (2020) by jakelazaroff on Tuesday May 25, 2021 @11:18AM (#27277948)
Conversely, fixing duplication is never hard. Just move the duplicated code into a function. Going in reverse can be much tougher if the function has become an abstraction, where you have to figure out what path each function call was actually taking.

Or, put another way: I'd much rather deal with duplication than with coupling problems.

Re:It's probably time to stop recommending Clean Code (2020) by bluGill on Tuesday May 25, 2021 @03:11PM (#27281089)
The problem with duplication it is hard to spot and fix. Converting liters to ml or quarts isn't hard, but the factors are different, and there is also other units. If you only do a few of these isn't a big deal, but if you suddenly realize that you have tons of different conversions scattered around and you really need to implement a good unit conversion system it will be really hard to retrofit everything. Note that even if you have a literToml, Literto Quart and MileToKm functions retrofitting the right system will be hard. (Where I work we have gone through 4 different designs of a uber unit system module before we actually got all the requirements right, and each transition was a major problem)
Re:It's probably time to stop recommending Clean Code (2020) by munificent on Tuesday May 25, 2021 @01:15PM (#27279521)
> Conversely, fixing duplication is never hard. Just move the duplicated code into a function.

I think the single biggest factor determining the difficulty of a code change is the size of the codebase. Codebases with a lot of duplication are larger, and the scale itself makes them harder to deal with. You may not even realize when duplication exists because it may be spread throughout the code. It may be difficult to tell which code is a duplicate, which is different for arbitrary reasons, and which is different in subtle but important ways.

Once you get to a huge sprawl of code that has a ton of mostly-pointless repetition, it is a nightmare to tame it. I would much rather be handed a smaller but more intertwined codebase that only says something once.

Re:It's probably time to stop recommending Clean Code (2020) by jeremysalwen on Tuesday May 25, 2021 @11:43AM (#27278313)
I think the opposite is true. Bad abstractions can be automatically removed with copy/paste and constant propagation. N pieces of code that are mostly the same but have subtle differences have no automatic way to combine them into a single function.
Re:It's probably time to stop recommending Clean Code (2020) by 0xffff2 on Tuesday May 25, 2021 @12:23PM (#27278831)
N pieces of code that are mostly the same but have subtle differences isn't repetition and probably shouldn't be combined into a single function, especially if the process of doing so it non-obvious.
Re:It's probably time to stop recommending Clean Code (2020) by dragonwriter on Wednesday May 26, 2021 @05:46AM (#27288321)
> N pieces of code that are mostly the same but have subtle differences isn’t repetition

Often, they are.

IME, a very common pattern is divergent copypasta where – because there is no connection once the copying occurs – a bug is noticed and fixed in one place and not in others, later noticed separately and fixed a slighly different way in some of the others, in still others a different thing that needs done in the same function gets inserted in between parts of the copypasta, etc. IT’s still essentially duplication – more over its still the same logical function being performed different places, but in slightly different ways, creating a singificant multiple on maintainance cost, which – not literal code duplication in and of itself, is the actual problem addressed with DRY, which is explicitly not about duplication of code but single source of truth of knowledge: “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system”. Divergent implementations of the same logical function are different representations of the same knowledge.

Re:It's probably time to stop recommending Clean Code (2020) by bluGill on Tuesday May 25, 2021 @03:21PM (#27281263)
Often it is. Earlier in this thread I used the example of a unit system - one example of there there can be a ton of repetition to remove, but there are fundamental differences between liters and meters that make removing the duplication hard if you didn't realize upfront you had a problem. Once you get it right converting meters to chains isn't that hard (I wonder how many reading even know chain was a unit of measure - much less have any idea how big it is), but there are a ton of choices to make it work.
Re:It's probably time to stop recommending Clean Code (2020) by jakelazaroff on Tuesday May 25, 2021 @12:36PM (#27279007)
I think they mean if the code does the same thing but has syntax differences. Variables are named differently, one uses a for loop while the other uses functional list operations, etc.
Re:It's probably time to stop recommending Clean Code (2020) by Too on Sunday May 30, 2021 @01:25AM (#27330833)
You never know if these subtle differences were intentional to begin with. It might have been the same once upon a time, but then during an emergency outage someone makes a quick fix in one place but forgets to update all other copies of this code.

Repeating what others already mentioned, often it can be the same thing but written in a slightly different way. Even basic stuff like string formatting vs string concatenation can make it non-obvious that two pieces of code are copies.

Re:It's probably time to stop recommending Clean Code (2020) by fiddlerwoaroof on Tuesday May 25, 2021 @11:33AM (#27278178)
The issue I have is that duplication is a coupling problem, but there’s no evidence in the coupled parts of the code that the coupling exists. It can be ok on a single-developer project or if confined to a single file, but my experience is that it’s a major contributor to unreliability, especially when all the original authors of the code have left.
Re:It's probably time to stop recommending Clean Code (2020) by kaba0 on Wednesday May 26, 2021 @05:40AM (#27288270)
If you find a bug in the duplicated part and has no idea that it was actually duplicated (or even if you do, where are they?), you still have multiple lurking bugs around.
Re:It's probably time to stop recommending Clean Code (2020) by bcrosby95 on Tuesday May 25, 2021 @07:15PM (#27284007)
Fixing duplication is never hard because by nature, duplicated code will drift over time even if it shouldn't have. So it's technically not "duplicate" anymore even if they are supposed to do the exact same thing.

Fixing a bad abstraction is only hard because there's some weird culture about not tearing down abstractions. Rip them apart and toss them on the heap. It's a million times easier than finding duplicate code that has inadvertently drifted apart over time.

Re:It's probably time to stop recommending Clean Code (2020) by fiddlerwoaroof on Tuesday May 25, 2021 @09:26PM (#27285038)
There are certain abstractions that can cause real problems: XML-driven DI, bad ORMs, code generators, etc. But, in general, I agree: people are generally too unwilling to refactor aggressively.
Re:It's probably time to stop recommending Clean Code (2020) by SassyGrapefruit on Tuesday May 25, 2021 @11:16AM (#27277922)
Yep. Re-use is difficult. When you overdo it you cause a whole new set of problems. I once watched a person write a python decorator that tried unify HTTP header based caching with ad hoc application caching done using memcached.

When I asked exactly what they were trying to accomplish they kept saying "they didn't want caching in two places". I think anyone with experience can see that these items are unrelated beyond both having the word "cache"

This is what was actually hanging them up ... the language. Particularly the word "cache". I've seen this trap walked into over and over. Where a person tries to "unify" logic simply because two sets of logic contain some of the same words.

Re:It's probably time to stop recommending Clean Code (2020) by bradstewart on Tuesday May 25, 2021 @11:10AM (#27277837)
So much this. Especially early in project's life when you aren't sure what the contexts/data model/etc really need to be, so much logic looks the same. It becomes so hard to untangle later.
Re:It's probably time to stop recommending Clean Code (2020) by watwut on Tuesday May 25, 2021 @11:16AM (#27277926)
Then you copy function and modify one place? I don't get what is so hard about it. The IDE will even tell you about all places where function is called, there is no way to miss one.
Re:It's probably time to stop recommending Clean Code (2020) by ummonk on Tuesday May 25, 2021 @12:30PM (#27278935)
This, exactly this
Re:It's probably time to stop recommending Clean Code (2020) by pjc50 on Tuesday May 25, 2021 @11:15AM (#27277913)
> it is _much_ easier to refactor copy pasta code

So long as it remains identical. Refactoring almost identical code requires lots of extremely detailed staring to determine whether or not two things are subtly different. Especially if you don't have good test coverage to start with.

Re:It's probably time to stop recommending Clean Code (2020) by macNchz on Tuesday May 25, 2021 @12:13PM (#27278686)
I personally love playing the game called "reconcile these very-important-but-utterly-without-tests sequences of gnarly regexes that were years ago copy-pasted in seven places and since then refactored, but only in three of the seven places, and in separate efforts each time".
Re:It's probably time to stop recommending Clean Code (2020) by tuatoru on Tuesday May 25, 2021 @11:52AM (#27278419)
> requires lots of extremely detailed staring

Yeah, nah. There are diffing tools.

Re:It's probably time to stop recommending Clean Code (2020) by cerved on Tuesday May 25, 2021 @12:24PM (#27278836)
problem is you only diff if things look different
Re:It's probably time to stop recommending Clean Code (2020) by nodoodles on Tuesday May 25, 2021 @11:04AM (#27277745)
> Write Code. Mostly functions. Not too much.

Just wanted to appreciate the nod to good nutritional advice here ("Eat food. Mostly plants. Not too much"), well done

Re:It's probably time to stop recommending Clean Code (2020) by latk on Tuesday May 25, 2021 @11:58AM (#27278494)
Probably a call-back to this post that made the rounds on HN a few months ago: https://www.brandons.me/blog/write-code-not-too-much-mostly-...
Re:It's probably time to stop recommending Clean Code (2020) by cerved on Tuesday May 25, 2021 @01:16PM (#27279533)
There's a problem with being overly zealous. It's entirely possible to write bad code, either being overly dry or copy paste galore. I think we are prone to these zealous rules because they are concrete. We want an "objective" measure to judge whether something is good or not.

DRY and WET are terms often used as objective measures of implementations, but that doesn't mean that they are rock solid foundations. What does it mean for something to be "repeated"? Without claiming to have TheGreatAnswer™, some things come to mind.

Chaining methods can be very expressive, easy to follow and maintain. They also lead to a lot of repetition. In an effort to be "DRY", some might embark on a misguided effort to combine them. Maybe start replacing

  `map(x => x).reduce(y, z => v)` 
with

  `mapReduce(x => x, (y,z) => v)`
This would be a bad idea, also known as Suck™.

But there may equally be situations where consolidation makes sense. For example, if we're in an ORM helper class and we're always querying the database for an object like so

  `objectContext.Orders.Select(e => e.id = y).Include(e => e.Customers).Include(e => e.Bills).Include(e => e.AwesomeDogs)...`
then it with make sense to consolidate that into

  `orderIncludingCustomersBillsAndDogs(id) => ...`
My $0.02:

Don't needlessly copy-pastes that which is abstractable.

Don't over abstract at the cost of simplicity and flexibility.

Don't be a zealot.

Re:It's probably time to stop recommending Clean Code (2020) by macNchz on Tuesday May 25, 2021 @11:39AM (#27278259)
>it is _much_ easier to refactor copy pasta code

I totally agree assuming that there will be time to get to the second pass of the "write everything twice" approach...some of my least favorite refactoring work has been on older code that was liberally copy-pasted by well-intentioned developers expecting a chance to come back through later but who never get the chance. All too often the winds of corporate decision making will change and send attention elsewhere at the wrong moment, and all those copy pasted bits will slowly but surely drift apart as unfamiliar new developers come through making small tweaks.

Re:It's probably time to stop recommending Clean Code (2020) by _huayra_ on Tuesday May 25, 2021 @11:43AM (#27278310)
I worked on a small team with a very "code bro" culture. No toxic, but definitely making non-PC jokes. We would often say "Ask your doctor about Premature Abstractuation" or "Bad news, dr. says this code has abstractual dysfunction" in code reviews when someone would build an AbstractFactoryFactoryTemplateConstructor for a one-off item.

When we got absorbed by a larger team and were going to have to "merge" our code review / git history into a larger org's repos, we learned that a sister team had gotten in big trouble with the language cops in HR when they discovered similar language in their git commit history. This brings back memories of my team panicked over trying to rewrite a huge amount of git history and code review stuff to sanitize our language before we were caught too.

Re:It's probably time to stop recommending Clean Code (2020) by morbicer on Tuesday May 25, 2021 @01:05PM (#27279391)
Wait whaat? I am not from USA and hail from a much more blunt culture, what's wrong with those 2 statements?

To me they're harmless jokes but maybe someone will point out they're ableist?

Re:It's probably time to stop recommending Clean Code (2020) by jasonwatkinspdx on Tuesday May 25, 2021 @02:29PM (#27280436)
Those aren't particularly bad, but dick jokes have no place in a professional workplace imo. I'm so very weary of it. And yes, in the past I was part of the problem. Then I grew up.
Re:It's probably time to stop recommending Clean Code (2020) by _huayra_ on Tuesday May 25, 2021 @01:15PM (#27279529)
It wasn't just these phrases, but we took them out to be safe (thinking that them being jokes about male... "performance", like from pharma ads on TV with an old man throwing a football through a tire swing).

The biggest thing is that we used non-PC language like "retarded" very casually, not to mention casually swearing in commit messages e.g. "un-fucking up my prior commit". Our sister team got in trouble for "swears in the git commit history", so we wanted to get ahead of that if possible.

In a healthy company culture, we'd just say "okay we'll stop using these terms", but the effort was made to erase their existence because this was a company where non-engineering people (e.g. how well managers and HR liked you) was a big factor in getting promoted.

Once I realized how messed up that whole situation was, I left as fast as I could.

Re:It's probably time to stop recommending Clean Code (2020) by icedchai on Tuesday May 25, 2021 @11:59AM (#27278513)
If you were "caught", what would happen? You probably have a couple of zoom calls and get forced to watch sensitivity training videos. Who cares.
Re:It's probably time to stop recommending Clean Code (2020) by dragonwriter on Tuesday May 25, 2021 @01:15PM (#27279526)
> it is _much_ easier to refactor copy pasta code,

Its easy to refactor if its nondivergent copypasta and you do it everywhere it is used not later than the third iteration.

If the refactoring gets delayed, the code diverges because different bugs are noticed and fixed (or thr same bug is noticed and fixed different ways) in different iterations, and there are dozens of instances across the code base (possibly in different projects because it was copypastad across projects rather than refactored into a reusable library), the code has in many cases gotten intermixed with code addressing other concerns...

Re:It's probably time to stop recommending Clean Code (2020) by haspok on Tuesday May 25, 2021 @12:09PM (#27278638)
> Write Code. Mostly functions. Not too much.

Think about data structures (types) first. Mostly immutable structures. Then add your functions working on those structures. Not too many.

Re:It's probably time to stop recommending Clean Code (2020) by throwaway2037 on Tuesday May 25, 2021 @01:53PM (#27279991)
OMG. This is exactly my experience after trying to write code first for 10+ years. (Yes, I am a terrible [car] driver, and a totally average programmer!)

"Bad programmers worry about the code. Good programmers worry about data structures and their relationships." - Linus Torvalds

He wasn't kidding!

And the bit about "immutable structures". I doubted for infinity-number-of-years ("oh, waste of memory/malloc!"). Then suddenly your code needs to be multi-threaded. Now, immutable structures looks genius!

Re:It's probably time to stop recommending Clean Code (2020) by beckingz on Tuesday May 25, 2021 @11:27AM (#27278093)
"Write Code. Mostly Functions. Not too much" made my day and is excellent advice.