3 ms·
I was looking for a word to describe my feeling about the article and "amateurish" fits the bill. What mostly took me down was: (for example, the same several
by ozim 3y ago
I was looking for a word to describe my feeling about the article and "amateurish" fits the bill.
What mostly took me down was: (for example, the same several lines of code duplicated across distant parts of the codebase dozens of times, and with inconsistent names which make the duplication hard to notice or track down)
It is silly example because in such scenario there is no way you can even start writing abstraction to handle that.
Other part is what cogman10 wrote, wrong abstraction is not "simply piece of code gathering if statements". Wrong abstraction is piece of code or whole part of system where you cannot simply add an if statement and get going. Wrong abstraction might be something that actively prevents you from changing code in meaningful way.
There is also another comment I would riff off about DevOps and having scripts per team/domain even if mostly those look the same you never know what the team will require. Nowadays domain driven development is in vouge, mostly because it recognizes separation of concerns is much more important than DRY.
To finish off, author also assumes abstractions are born by de-duplication of code, yes we discuss "duplication is cheaper" so as finishing wanted to rant on something. Worst abstractions I saw in practice were born in heads of "Astronaut Architects" who built some system top down making stuff up "because it should be like that". Other bad ones were done by junior devs who were high on DRY.
- laserlight 3y ago> It is silly example because in such scenario there is no way you can even start writing abstraction to handle that. You start writing the abstraction the first time you duplicate, so that you don’t end up with this mess down the road.
- kiitos 3y agoBetter rule is the first time you duplicate, just copy/paste the code. But once you duplicate a third time, that's a pretty good signal that you have something that is common enough to be abstracted, and you can write the abstraction then.
- laserlight 3y agoIn my experience, one is unlikely to remember how many times a piece of code is being copy-pasted, unless they are doing it in the same coding session. Everyone copy-pastes, thinking that they'll DRY the next time and the codebase turns into a mess.
- kiitos 3y agoWell, when you make a new abstraction, that same PR will presumably also include changes to some N call sites to make use of that abstraction, right? The important thing is that N>=3. Anything less than that is, more often than not, premature. And in my experience, premature and/or leaky abstractions are far (far) more harmful to codebases than copy-pasted code.
- laserlight 3y agoI abstract, i.e, put code into its own function, even when N=1, to make surrounding code more readable and simpler. At N=2, I definitely abstract. Abstracting doesn't mean building class hierarchies, when they are not needed. I abstract at the required level. Premature abstraction, in my book, means abstracting in expectation of future needs. It is different than abstracting small and often. My abstractions are not premature, because they address existing needs. I view copy-pasted code as cancer of a codebase. It is very difficult to tell if two similar looking code function the same way, therefore making it practically impossible to DRY away, whereas calling the same function leaves no room for guess. Copy-pasted code reduces readability, understandability, makes code longer. It never happens under my supervision.
- kiitos 3y agoGiven fn a foo a foo b foo c bar d bar e baz f baz g baz h and fn b foo bar baz fn foo a b c fn bar d e fn baz f g h which of a or b is more readable and simpler? (Spoiler: it's almost always a.) DRY isn't about repetition of text in a source file, it's about repetition of authority in your domain model. You evaluate it at an architectual level, not in PR diffs or whatever.