News for hackers. Stuff that matters.
Re:That coworker who never stops refactoring by simonw on Tuesday August 11, 2020 @10:40AM (#24120718)
A big risk here is that it's very easy for refactoring you make a codebase worse.

Someone spots the same functionality in two places, applies DRY, then two weeks later a new requirement means they need to work slightly differently from each other and that change has to be unwound again.

I've seen plenty of helpful refactors that broke code in subtle ways (if your tests aren't robust enough this can happen really easily).

I've seen projects take literally months longer because the team hit caught up in "refactoring" code written by previous engineers - not because it needed it, but because the new engineers preferred to write their own code than dig in and understand what was written before them.

Re:That coworker who never stops refactoring by thinkharderdev on Tuesday August 11, 2020 @10:47AM (#24120812)
Maybe it's just me but in my career (15 years at this point as a professional software engineer) by FAR the biggest problem is teams not doing enough refactoring. When new features come along that violate basic assumptions of the initial codebase, instead of refactoring out those initial assumptions you hack around them. It's almost always faster to do that then do a proper refactor for a particular feature but over time the codebase devolves into a tangle of spaghetti and incomprehensible chains of if/then/else where variable names have no semantic connection to how they are being used anymore. Sure, you can have the opposite problem where you refactor too eagerly and waste time but all the incentives in a corporate setting work against erring on the side of too much refactoring.
Re:That coworker who never stops refactoring by sigy on Tuesday August 11, 2020 @07:27PM (#24127128)
I find the article quite presumptive in the sense that it presumes everyone has the same definition for "gold plated", etc. That, coupled with the With the remedy of "give the guy something else to do" makes me think that the writer is on the opposite of the spectrum which could be characterized as "throw stuff at the wall and see what sticks."

I know this seems pejorative on some level, but I'm trying to paint a contrast here between sensibilities. I think it is worth considering that one person's urgent and long-needed reduction in technical debt is another person's over-engineered. It often comes down to whether the person passing judgement has the awareness of how everything fits together and thus can benefit from a structural tune-up. It could also depend on how many systems and code bases they have had to maintain.

I mean, on balance, we have a really bad track record as an industry of building unmaintainable code bases. Most developers are working on the things they find enjoyable with less regard for making it work well for other developers. I think that the author's remedy of splitting these responsibilities out into separate roles is an indicator of the overall problem. It shouldn't have to be framed in that way to make it palatable to the average developer.

As a developer, you should always want to make the code you work on work well for others, and that includes refactoring on occasion. If you find yourself in a posture where you are always "innovating the new fun stuff" while others are seemingly dragging you down with making it actually manageable, it's time to slow down enough to realize what this means to your team.

By not taking care of your own code so that your other developers find it manageable, you've turned them into your cleanup crew. You need to thank them for being "That coworker" who you find "gold plater, or unproductive, or slow", and perhaps stop building prototypes and calling them done.

Just some food for thought.

Re:That coworker who never stops refactoring by Consultant32452 on Tuesday August 11, 2020 @11:12AM (#24121132)
Don't forget halfway refactoring. The worst case I've ever had was a project with 3 completely different and incompatible ways of accomplishing everything. Different packaging schemes, different DAO designs, different UI framework/libraries. The worst!
Re:That coworker who never stops refactoring by gHosts on Tuesday August 11, 2020 @05:42PM (#24126364)
The technical term for that is "a Lava Flow".

The code melts and changes for awhile... and then congeals into unchanging rock again.

Re:That coworker who never stops refactoring by lstamour on Tuesday August 11, 2020 @10:01PM (#24127923)
I'm using that term from now on. :)

One project I worked on had almost a half dozen "lava flows" in the JavaScript alone... I had a hard time convincing folks we could rewrite it using ASTs because all the rewrites scared folks away from any further rewrites!

Re:That coworker who never stops refactoring by Joker_vD on Wednesday August 12, 2020 @06:57AM (#24130643)
The best (well, worst, really) part is when it ends in a state where the parts are almost right, but not quite. The result is, quote one of my coworkers, "We ended up with the architecture that is so flexible that we can't change a single bloody thing in it! What the hell!" The whole is flexible, but this flexibility is pointless: you can make the parts behave differently, but there is only one behaviour that is actually useful; you can combine things together differently, but there are at most two combinations that do things in a useful way; etc.
Re:That coworker who never stops refactoring by flavious on Thursday August 13, 2020 @03:30AM (#24140849)
Sounds like the effects of temporal coupling, or "settism and gettism", or other principles, which are violated. People are blinded nowaways too much by SOLID, and forget about the remaining 20 principles.
Re:That coworker who never stops refactoring by mr_coleman on Wednesday August 12, 2020 @04:45AM (#24129823)
I've seen that applied to long running legacy projects. This is how they did things back in 2008. Here someone discovered an orm library. Now here, they started to try to introduce some microservice event driven architectural pattern that was all the rage in the blogs that year....
Re:That coworker who never stops refactoring by thinkharderdev on Tuesday August 11, 2020 @11:19AM (#24121210)
I think we can all agree that the dev who starts a big refactor and then drops it when it's half-done is a terrible human being
Re:That coworker who never stops refactoring by bradstewart on Tuesday August 11, 2020 @11:40AM (#24121534)
I don't agree. Big refactors are _hard_. They usually require levels of sustained focus at multiple layers of abstraction, and its really easy to get exhausted/burnt out or be forced to switch gears in the middle.

People learn by doing. And after a few such messes, the dev will learn how to do smaller refactors.

Re:That coworker who never stops refactoring by droobles on Tuesday August 11, 2020 @01:04PM (#24122688)
That's me. I want to finish my refactor but business requirements have me pumping out new epics.

I chip away at it in my down time but it's no longer prioritized.

I find in this line of work, you will always seek to find ways to balance business/market needs vs. being a paragon of programming.

Re:That coworker who never stops refactoring by mcv on Tuesday August 11, 2020 @06:35PM (#24126799)
There's not always time to finish it properly, but there's often a good stopping point halfway. When there's a lot that needs refactoring, I start with one specific thing, and leave the rest for now, so the piece I've bitten off is still small enough that it can be wrapped up in a reasonable amount of time. And the rest will have to wait.

In my current project, there are piece of code that I've wanted to remove for months now. I was working on refactoring it out completely, but there's one place that still uses it, and that wasn't so easy to fix as all the other places where it had become unnecessary. It still needs to be done, but there hasn't been any time. Or urgency, really. But it's still there, taking up space in my head.

Re:That coworker who never stops refactoring by flavious on Thursday August 13, 2020 @03:32AM (#24140861)
And how does the team feel about it?
Re:That coworker who never stops refactoring by mcv on Thursday August 13, 2020 @08:25AM (#24142623)
They also want to get rid of the old code, but they're not removing it. One of them also has a tendency to interesting structural solutions, though he tends to do it more by adding them than by rewriting what was already there, which has the advantage that it doesn't disrupt the old code that others are working on, and the corresponding disadvantage that the old code survives.
Re:That coworker who never stops refactoring by revel on Tuesday August 11, 2020 @10:55AM (#24120915)
Similar experience and totally agree. How often do people go out of their way to improve a severely limiting code base? Not enough.
Re:That coworker who never stops refactoring by LargeWu on Tuesday August 11, 2020 @11:01AM (#24121005)
Often it's because they are being pressured by leadership not to. Leadership's bonuses and promotions are dependent on developers delivering value NOW.
Re:That coworker who never stops refactoring by hhas01 on Tuesday August 11, 2020 @12:30PM (#24122218)
Some of that is on developers, famous for overpromising and under-delivering and generally not managing expectations. There is a real skill to effectively explaining a job’s requirements to non-developers, and it’s something I think a lot of us suck hard at.

Plenty of it is also on managers who simply aren’t competent to manage, of which there is a tragic abundance. If that really bothers you, move up to management and start doing the job better or go find a new job. Otherwise, not your circus, not your monkeys.† Just make sure to keep your papertrail.

--

† Whereas the liabilities for screwing with production code outside your scope of work def should be. Though again, that assumes a management that actually understands what’s going on in its own shop, and isn’t just running around with its hair on fire 24/7.

Re:That coworker who never stops refactoring by sukilot on Tuesday August 11, 2020 @11:28PM (#24128353)
Doesn't leadership hold a lot of stock?
Re:That coworker who never stops refactoring by nthj on Tuesday August 11, 2020 @11:05AM (#24121052)
Most productive refactoring carries forward from a more effectively designed data model. There can be value in pushing code around but often a lot of that can be subjective, which this thread has discussed.

Even big corporations are open to refactoring when you put it in the context of data integrity and consistency (foreign keys), because data inconsistency causes tickets which cause churn which is a number the CEO reports to the board.

Re:That coworker who never stops refactoring by thinkharderdev on Tuesday August 11, 2020 @11:17AM (#24121192)
Mostly agree but I think the distinction is not always clear between data model changes and changes to application-tier logic. In most cases you can do a particular refactor either way and you have to figure out what is the best way to achieve it. Just pushing code around is usually not tremendously valuable but it's also not particularly costly.
Re:That coworker who never stops refactoring by xupybd on Tuesday August 11, 2020 @08:50PM (#24127586)
This is what I've seen too. Higher pressure to get features out. Then the code gets worse and worse over time. Development gets slower and pressure gets more intense. Then customer gets pissed off and fault lands on the Devs.
Re:That coworker who never stops refactoring by _8ljf on Tuesday August 11, 2020 @12:10PM (#24121972)
Yep, but there you are refactoring as a necessary prerequisite to achieving a clearly-defined end goal. That’s part of the development roadmap and should be planned and budgeted accordingly. When building a house, a professional builder knows to dig out and pour a solid foundation, so that by the time they get to tiling the roof the walls beneath it aren’t already sinking and tearing apart.

That’s very different to just dicking with the company codebase for one’s personal amusement. That’s the bloke with the dozen rusted automobile shells sitting on bricks in his front yard, while he’s in his shed “busy working” on number thirteen. He’s not productive, he’s just playing with himself. And making the whole place look like trash while he’s at it.

Re:That coworker who never stops refactoring by thinkharderdev on Tuesday August 11, 2020 @01:04PM (#24122691)
Right, of course dicking around with the codebase for personal amusement is bad and a poor use of time. But I think refactoring doesn't necessarily have to be achieving a clearly defined end goal to be useful and appropriate. Primary because product development itself doesn't have a clearly defined end goal. It's an iterative process where the end goal can change as you discover new information. To follow on your example, if I'm building a two story house and pour a foundation to support that but then the foreman comes along and says "actually we need to build a ten story office building on the same footprint" then you absolutely should start over and pour a new foundation. If you have a well-defined spec to begin with and it doesn't change and you STILL need to do a bunch of refactoring along the way then you probably did something wrong. But in my experience that is almost never the case. Refactoring happens because the requirements change and assumptions you made are no longer valid.
Re:That coworker who never stops refactoring by vvanders on Tuesday August 11, 2020 @10:41AM (#24120733)
Yup, instead of DRY I'm a big fan of WET, Write Everything Twice.

Third time? Maybe consider a refactor at that point.

Re:That coworker who never stops refactoring by weego on Tuesday August 11, 2020 @10:51AM (#24120860)
I learned this from the smartest dev manager I've ever had in the second or third week at the job, it really tore down a lot of the notions I'd formed as a self taught developer.

We were looking at a pull I'd created and he asked 'are these things really the same or is it just a coincidence because the features aren't mature enough yet?'. And yes I'd conflated two things with entirely different intents by over-abstracting.

Re:That coworker who never stops refactoring by landryraccoon on Tuesday August 11, 2020 @10:54AM (#24120896)
Wow, you just blew my mind. "Don't Repeat Yourself" could just be premature optimization and overfitting on a tiny data set.
Re:That coworker who never stops refactoring by gsej on Wednesday August 12, 2020 @08:42AM (#24131448)
Absolutely. This has been discussed here quite a bit (e.g. https://news.ycombinator.com/item?id=120614530). In real life it's often visible when you have methods that take one or more flags to tell them how to behave. Fixing involves duplicating the method - one copy for calls where the flag is true, one for where it is false. Then simplifying, then repeating. The code that falls out at the end is often dramatically simpler than what was there before. It makes me wonder about some of these programming aphorisms - they only seem to work if you are already experienced enough to know to take them with a pinch of salt.
Re:That coworker who never stops refactoring by thehappypm on Wednesday August 12, 2020 @03:51PM (#24136063)
DRY is really valuable for noobs. Like, defining magic numbers twice in adjacent functions is really common behavior for beginners. Likewise, writing the same exact logic several times in different parts of the code without encapsulating into a function. Stuff that would be obvious for even junior devs.

But I do think it falls apart when applied at a more macro level.

Re:That coworker who never stops refactoring by k_sze on Tuesday August 11, 2020 @12:21PM (#24122104)
The book "Pragmatic Programmer" talks about this in chapter 2, topic 9.

The kind of duplication you want to avoid is knowledge duplication. If two pieces of code literally do the same thing but for different reasons/purposes, it may not be a good idea to deduplicate them.

Re:That coworker who never stops refactoring by WalterBright on Tuesday August 11, 2020 @07:34PM (#24127175)
Very early versions of the my compiler's code generator were set up to minimize code size, for the simple reason that 640K wasn't enough. This meant merging disparate sequences of code, often with goto's.

The result made it hard to understand the code, and hard to graft in new functionality. So over time I refactored it to duplicate the code, but each string of code is a lot easier to understand.

Re:That coworker who never stops refactoring by mcv on Tuesday August 11, 2020 @06:43PM (#24126860)
I've got the bunch of services that all do the same thing on a different data model. Originally they were all different and completely custom. Over time, I've managed to get nearly all of the code practically identical across 8 services. The same code is duplicated 8 times, but I prefer that over when they were all different, because then they were all doing basically the same thing in 8 different ways.

I know that eventually I can move all the identical code out of there to its own service, and replace the 8 services with just 8 simple configuration lists, and that's definitely where we're going at some point in the future, but for now, the 8 times duplication is actually fine. People know these services and what they do, and I know they're conceptually all instances of the same thing, it's just not quite represented that way in the code yet. We'll get there.

It's possible a few still have some exceptions, so that needs to be figured out first, but due to the way the code is put together, there's no rush; people are only changing the parts they're meant to change anyway.

Re:That coworker who never stops refactoring by lkschubert8 on Tuesday August 11, 2020 @06:34PM (#24126791)
Yeah I think DRY has been bastardized to mean dont repeat code when it actually means encode business logic in one place.
Re:That coworker who never stops refactoring by mcv on Tuesday August 11, 2020 @06:45PM (#24126878)
I mind duplicated code a lot less than duplicated data. If two things have to match, but they're using two different configurations, then there's going to be a mismatch some day. I'd rather create the second config out of the first one. If front- and backend need to know something, have the front-end get its config from the backend, instead of hard-coding it in two places.
Re:That coworker who never stops refactoring by fiddlerwoaroof on Tuesday August 11, 2020 @10:56AM (#24120930)
I’ve inherited codebases like this, they almost always have subtle bugs because people changed something but missed the other place the logic needed to be modified. My thought is that functionally cohesive blocks of code should almost always be extracted to a function and named: if this causes a bad abstraction, you can always either duplicate the function and rename or inline the function and re-abstract.
Re:That coworker who never stops refactoring by LargeWu on Tuesday August 11, 2020 @11:04AM (#24121030)
I've had the opposite experience: bad abstractions generally cause more pain than duplicated code. Generally this is because bad abstractions tend to leak their bad-ness into the code around them.
Re:That coworker who never stops refactoring by alephu5 on Tuesday August 11, 2020 @06:26PM (#24126729)
People think I'm a charlatan when I advocate copy and pasting code around, but really it just takes a few seconds so I don't think time concerns are a good argument against it.

Rather you should ask yourself if you want these two functions to be coupled? So that by changing one you are forced to change both. This is very desirable in some cases, but when you don't want components bolted together just copy and paste.

Re:That coworker who never stops refactoring by BigJono on Wednesday August 12, 2020 @03:10AM (#24129351)
I think the problem with copy pasting is that it implies you're copying a chunk big enough to be worth taking the shortcut. If you happen to have two of the same two-liners, then there's a good chance they shouldn't be abstracted. But if they're five-liners that probability goes way down.

I'd guess that most devs don't c/p two lines, unless they're particularly slow typers. Or you're one of those fancy vim people that jumps all over the joint with keystrokes instead of having to move to the mouse to select text.

Also, the reason I basically never c/p is because I do it manually. I have a habit of using old code as a reference while I write new code, and the two pieces of code somehow end up being the same. The difference is, I've vetted every single line of what I've written, even if it's a duplicate, so I know it's relevant to the new context I'm working in. If you c/p, there's no requirement for you to mentally check that the new code is all relevent, and correct, for the new context.

Re:That coworker who never stops refactoring by sytringy05 on Tuesday August 11, 2020 @09:18PM (#24127709)
This is fine until the copy/paste paradigm escapes the sensible boundary.

Copying the code - 2 seconds. Copy the unit tests? Another 2 seconds? Refactoring the copy pasted thing that is now in 25 different git repos? Sad face

Re:That coworker who never stops refactoring by Akronymus on Tuesday August 11, 2020 @11:13AM (#24121145)
Oftentimes it is better to make code that is easily copy pasted/edited later than to make reusable code IMO.

I think knuth was the first time I heard of reeditable.

For example I had to recently write some code to center a form shown as a dialog based on the overlying window. Because we use a MDI. I had to take into account local to screenspace coordinates.

Once it was a usercontrol embedded in a form, once just a form and once the top window, etc. All of those needed just tiny adjustments, while rolling it into one function would be MUCH more cumbersome.

Re:That coworker who never stops refactoring by shortcord on Tuesday August 11, 2020 @11:25AM (#24121301)
I prefer Write Everything Thrice.
Re:That coworker who never stops refactoring by rmetzler on Tuesday August 11, 2020 @10:59AM (#24120976)
Thanks for adding that acronym to my vocabulary. I use this strategy for a long time but needed more words to explain it.
Re:That coworker who never stops refactoring by BurningFrog on Tuesday August 11, 2020 @11:03AM (#24121015)
Re:That coworker who never stops refactoring by un_montagnard on Wednesday August 12, 2020 @01:07AM (#24128753)
I have seen code based that were following this principle, but nobody realised they were writing the same thing for the 5th time.
Re:That coworker who never stops refactoring by frompdx on Tuesday August 11, 2020 @06:16PM (#24126666)
If I recall correctly this is also the advice PG gives in On Lisp when deciding whether or not it is time to write a new function.
Re:That coworker who never stops refactoring by thelastinuit on Tuesday August 11, 2020 @10:55AM (#24120917)
I just wet myself! Love this!
Re:That coworker who never stops refactoring by tejtm on Tuesday August 11, 2020 @08:30PM (#24127486)
just be sure to stop before it just becomes "We Enjoy Typing"
Re:That coworker who never stops refactoring by trey-jones on Tuesday August 11, 2020 @10:58AM (#24120954)
I think that's called technical debt.
Re:That coworker who never stops refactoring by Ericson2314 on Tuesday August 11, 2020 @07:36PM (#24127190)
> Someone spots the same functionality in two places, applies DRY, then two weeks later a new requirement means they need to work slightly differently from each other and that change has to be unwound again.

That sounds like a board fresh-out-of-school dev. An experienced "refactorist", who knows the risks such as pissing off the other team members who find their memory of the program expired, in addition to pissing off managers with extra latency, isn't going to waste precious political capital on dubious refactors.

Make more your senior devs start the refactor, or better yet, they start them, and junior devs have to finish them. Grueling but useful training exercise.

> I've seen projects take literally months longer because the team hit caught up in "refactoring" code written by previous engineers - not because it needed it, but because the new engineers preferred to write their own code than dig in and understand what was written before them.

I've seen that too.

But a good refactor can also be the antithesis of NIH syndrome: a good refactor means someone took the time to understand the old code enough to safely change it. A slap-on "write only" patch conversely may indicate someone didn't bother to understand what was already there.

It's important to distinguish between refactors that "kneed" the code, mostly moving existing bits around, and partial rewrites that replace one portion of the code with all-new stuff. The former is a lot more trust worthy.

Re:That coworker who never stops refactoring by cameronfraser on Tuesday August 11, 2020 @10:42AM (#24120743)
Well obviously don't refactor in a vaccum, part of an engineer's skillset is anticipating future changes and being flexible to them. You can still get DRY with slightly different functionality, most people with some OOP background should know how. Most of the points you bring up have nothing to do with refactoring and everything to do with other failed processes.
Re:That coworker who never stops refactoring by simonw on Tuesday August 11, 2020 @10:45AM (#24120778)
I'm not saying refactoring is bad. I'm saying that in my experience engineers who "never stop refactoring" (the topic of this post) frequently do it badly and cause more harm than they actual long-term code improvements.
Re:That coworker who never stops refactoring by cameronfraser on Tuesday August 11, 2020 @10:47AM (#24120802)
I have had the opposite experience which is more in line with the author of the article. Most coworkers I've had that refactor a lot were the best technical coders on the team and we're usually refactoring other people's short sightedness.
Re:That coworker who never stops refactoring by schwartzworld on Wednesday August 12, 2020 @08:12PM (#24138622)
If you do a lot of refactoring you get better at it. You learn to write code that is easily refactorable, so that it doesn't hurt to refactor it again later.
Re:That coworker who never stops refactoring by dognotdog on Tuesday August 11, 2020 @11:20AM (#24121221)
> A big risk here is that it's very easy for refactoring you make a codebase worse.

That seems to happen when refactorings are infrequent and random, vs. a well-groomed codebase. Of course the well-groomed codebase is a mythical creature.

The problem I see is that if refactoring isn't part of the culture, what tends to be built is a tower of cards that never gets the benefit of hindsight because with each subsequent change, the understanding of interactions decreases, and the fear of breaking something increases, leaving to very narrow cludgy changes that become impossible to maintain.

The added impetus of external schedules just exacerbates the issue until the whole codebase falls over, unless people come along who take on that risk and attempt live brain surgery.

Re:That coworker who never stops refactoring by jiveturkey on Tuesday August 11, 2020 @01:25PM (#24122969)
> than dig in and understand what was written before them

which would take nearly as long yet have more uncertainty.

everytime i see this, which is all the time, it's because engineers are not trained nor managed properly. with everyone doing "agile", and the wrong incentives in place, engineers write code that only lives until the next sprint.

you can't just blame the engineers. you need both sides of the whole to make it work. management has to place importance on quality, and engineers have to learn to write code for someone ELSE to read. (usually themselves, 6-12 months later).

the common, pervasive flaw i always see is the curse of knowledge. engineers write code that they understand, at the time they write it. when i review, i want to see code (rather, comments, for the most part) that don't expect specific and deep internal codebase knowledge. a one-line 10s comment that seems obvious at the time (due to the curse of knowledge), can save 10m later. when poring through the code 6 months later, each of those 10m chunks adds up, very very quickly.

Re:That coworker who never stops refactoring by watwut on Tuesday August 11, 2020 @10:57AM (#24120944)
> Someone spots the same functionality in two places, applies DRY, then two weeks later a new requirement means they need to work slightly differently from each other

In this particular case, it takes like 15 seconds to split those places ... even if you dont remember that they were ever split.

Re:That coworker who never stops refactoring by dasil003 on Tuesday August 11, 2020 @11:03AM (#24121016)
Let's take bad or short-sighted refactoring off the table and only consider good refactoring. Even in that case it's not obvious when refactoring is justifiable. If it's just one or two small features hacked onto a sub-optimal code base then probably not worth refactoring, but what if it's more? Well, you might want to actually build them first and then refactor once you have good test coverage and proven use cases. But wait, what if the old architecture is forcing a lot of incidental complexity that will be costly to unwind? In that case it may be better to pay the refactoring cost up front.

The point is that any engineer who is always refactoring or never refactoring is doing it wrong. All cases require judgement of the specific situation and balancing short-term goals with long-term vision.

Re:That coworker who never stops refactoring by schwartzworld on Wednesday August 12, 2020 @07:51PM (#24138488)
> the new engineers preferred to write their own code than dig in and understand what was written before them.

once you dig in and understand what was written before, that's the logical time to refactor. so the next guy doesn't have to spend the time

Re:That coworker who never stops refactoring by commandlinefan on Tuesday August 11, 2020 @06:53PM (#24126933)
> If your tests aren’t robust enough

In that case, let Fred focus on improving the unit tests.

Re:That coworker who never stops refactoring by gsej on Wednesday August 12, 2020 @08:46AM (#24131475)
Hmmm. Once Fred is solely responsible for all of the good engineering practises, we should probably rename him Brent (http://allengeer.com/how-to-handle-brent-in-the-phoenix-proj...)
Re:That coworker who never stops refactoring by amelius on Tuesday August 11, 2020 @11:19AM (#24121205)
Then that's a sign you should put more work in your test-suites.