News for hackers. Stuff that matters.
Re:Throw away your first draft of your code by kleiba on Friday August 04, 2023 @03:12PM (#37004461)
The temptation to throw away all of your code - be it a prototype or a "grown" code base - arises often.

Often it is a bad idea. I get it, though, there is an inherent attractiveness in the idea of starting fresh from a clean slate. Except, more likely than not, you'll soon find yourself in a similar situation to the one you started from. The fundamental problem is that it is easy to underestimate the edge cases. Sure, the main functionality is easily understood and straight-forward to conceptualize. But did you remember to think through all the smaller aspects of your software, too?

Perhaps it's easier with a prototype implementation that in fact doesn't have a lot of features yet, but to completely replicate the functionality of a complex piece of software isn't an easy undertaking. Sure, getting 80% there is probably easy, but the last 20% is the part that's easy to overlook when considering a complete rewrite.

Admittedly, there's nothing sexy about refactoring. And often it may seem like it's less work to just simply scrap everything and start over. However, that's fallacy a lot of times.

Re:Throw away your first draft of your code by josephg on Friday August 04, 2023 @03:39PM (#37004833)
> Admittedly, there's nothing sexy about refactoring. And often it may seem like it's less work to just simply scrap everything and start over. However, that's fallacy a lot of times.

Agreed. Though sometimes you need to refactor the core of an application, in a way which will touch the entire app. To do that I often make a new, empty project. Then I rewrite the core of my project into the new folder (in whatever new structure I’m trying out). When that works, I slowly copy the content from the old project into the new project - refactoring and testing along the way.

But it’s not perfect. About half the time I do this, I discover halfway through that I didn’t understand some aspect of the system. My new design is wrong or useless and I throw away all the new code. Or I figure out that I can just make the changes in place in the old project folder after all, and I bail on the new code and go back to a traditional refactor.

But no matter what happens, I don’t think I’ve ever regretted a refactoring attempt like this. I always come away feeling like I’ve learned something. How much you’ve learned from a project is measured by the number of lines of code you threw away in the process.

Re:Throw away your first draft of your code by majikandy on Friday August 04, 2023 @07:47PM (#37007351)
I find refactoring sexy. Just saying.
Re:Throw away your first draft of your code by eternityforest on Friday August 04, 2023 @03:38PM (#37004821)
I don't really feel that attraction to complete rewriting. I wonder if the people who do are very smart, and thus able to hold more state in their head, so ugly code bothers them more even if it's not actively a problem, because they are able to have background tasks in their mind to worry about it?

And at the same time, perhaps their code is less encapsulated, because they didn't optimize for abstraction, they optimized for beauty. A leaky abstraction doesn't bother them, because ALL abstractions are leaky to them, they probably have a sense of internal workings even whem using household appliances, but ugly code tucked away somewhere bothers them a lot, and they might dislike using popular large libraries even if they work great, just because they're not comfortable using what they don't understand deeply.

My evidence of this is the fact that suckless exists and people actually use it, I assume their experience of thought is very different from anything I have experienced.

Re:Throw away your first draft of your code by jwells89 on Friday August 04, 2023 @04:22PM (#37005359)
Speaking personally my urge to rewrite at least partially comes from not truly understanding the problem and the solutions to it until I've written something reasonably functional. It doesn't matter how much time I put into sitting and theorizing, there's always things I didn't anticipate and assumptions that turned out to be incorrect.

This usually means that rewrites are significant improvements across the board, especially if they're done a relatively short time after the original is finished since it's all still fresh in my head.

This may be a weakness of sorts on my part though, I lack formal engineering training which might be why purely mental modeling (no code) doesn't work all that well for me.

Re:Throw away your first draft of your code by extr on Friday August 04, 2023 @04:41PM (#37005596)
Yeah, I find that the desire to rewrite often comes from the final (working) solution having poor code ergonomics. It's not necessarily that it's wrong, it's that it's awkward or clumsy to understand/use, because my mental model of how it worked didn't take into account the actual practical day to day usage of the code.
Re:Throw away your first draft of your code by eternityforest on Saturday August 05, 2023 @03:20AM (#37009785)
I get around that by writing the API first, with usage code examples and all, Or in a GUI app, the UI first. I'll iterate there if needed, and only rewrite internals if it's truly bad or I think it will cause a problem later.

A lot of situations where the code is so nasty I actually have wanted to rewrite... I wind up just throwing it away instead.

Like, one of my happiest days in coding was throwing out some code to support JACK and going all in on Pipewire. Another happy day was replacing some DIY code with a GPL licensed library, deciding that I really didn't need the option to do proprietary stuff anywhere near as much as I wanted less thinking about gstreamer.

"If it's hard to explain, it's probably a bad idea".... If I try a bunch of API variations and I can't come up with anything that doesn't require learning 3 new algorithms to use... Maybe I'm not making a library or an abstraction, I'm making a proof of work hashing scheme that users have to do manually to access an encrypted version of the complexity I'm hiding, and I need to stop before I make z80 assembly in JSON to autogenerate Vue templates that render to cobol.

Re:Throw away your first draft of your code by ipaddr on Friday August 04, 2023 @05:10PM (#37005929)
Smart people can ignore ugly code. It's the people who get easily confused that need to see clean and easy to understand code.
Re:Throw away your first draft of your code by bigfudge on Friday August 04, 2023 @05:33PM (#37006183)
Sadly, I think this is true of me — at least of me. I don't think smart people (by this definition) are that common though, so clean code is a sensible default.
Re:Throw away your first draft of your code by alpaca128 on Friday August 04, 2023 @08:36PM (#37007700)
Humans always make mistakes and are tired now and then, and the smartest people can still appreciate not needing to solve puzzles just to understand or debug their own code.

Of course clean and readable means very different things to different people, but I don't thing I ever regretted cleaning up code, while I can think of a lot of instances where not doing it wasted a lot of time.

Re:Throw away your first draft of your code by nouveaux on Saturday August 05, 2023 @06:38AM (#37010743)
If other people need to read your email/doc/code, then any effort to make it more readable will save them time, regardless of how smart they are.

"If I had more time, I would have written a shorter letter."

Re:Throw away your first draft of your code by lamontcg on Friday August 04, 2023 @06:19PM (#37006633)
I'd rewrite code to make it simpler and easier to hold in one's head, not just to make it pleasing from some aesthetic viewpoint.
Re:Throw away your first draft of your code by freehorse on Friday August 04, 2023 @07:42PM (#37007312)
There is some point regarding changes on the code where rewriting is less costly or more costly than changing the current code. For me, a big part depends on whether I depend long term or short term on it and how deep I will have to go anyway using it.
Re:Throw away your first draft of your code by coolliquidcode on Friday August 04, 2023 @09:06PM (#37007893)
ALL abstractions are leaky - this is an objectively true statement.

As others said it's not for beauty, it to make sure if there is abstraction it fits the problem. If there is encapsulation it doesn't get in the way. For some coders they can get it on the first try and there is no reason for them to rewrite code. For the rest of us mid coders we need to explore first as well as make sure all cases we desire for Lib/API work.

Re:Throw away your first draft of your code by eternityforest on Friday August 04, 2023 @09:15PM (#37007945)
You're right, I should have been more clear, all abstractions are leaky, but some are right enough most only notice occasionally.
Re:Throw away your first draft of your code by nemetroid on Friday August 04, 2023 @03:32PM (#37004763)
Throwing away an established code base is a completely different thing from throwing away a few-days-old prototype, and not what the article is suggesting. The points you mention don't apply to the prototype case.
Re:Throw away your first draft of your code by tuwtuwtuwtuw on Friday August 04, 2023 @04:00PM (#37005082)
Yeah, this thread is a somewhat strange read. People don't seem to understand what "draft" or "prototype" means. Odd.
Re:Throw away your first draft of your code by redblacktree on Friday August 04, 2023 @04:50PM (#37005701)
Just like management. I can't tell you how many times I've written a "Proof of Concept" that became production code.
Re:Throw away your first draft of your code by doublerabbit on Friday August 04, 2023 @05:34PM (#37006197)
I did that today. Rewrote a bash script in to a perl script as Proof of Concept. it's now in UAT... but I like perl, hey ho.
Re:Throw away your first draft of your code by Vicinity9635 on Friday August 04, 2023 @09:30PM (#37008060)
To turn this around, a proof of concept is the fastest way to get code into prod ;)
Re:Throw away your first draft of your code by gillesjacobs on Saturday August 05, 2023 @06:23AM (#37010664)
This has caused me so much grief. I find it key to communicate to all stakeholders the production process from PoC to MVP to MVP+.

Still managers always push against redoing work they can see working for the happy flow.

Re:Throw away your first draft of your code by Tokumei-no-hito on Sunday August 06, 2023 @10:42PM (#37029418)
I feel this. What I do now is increase the points of the PoC (real estimated PoC time + optimization).

I get the PoC down asap then as soon as it’s proven (privately to myself) I move onto optimizing. If it fails then I communicate early to discuss a change of scope.

Assuming it succeeds, by the time I present it I have already done significant work making it production ready. So to request an extra 10-25% time to “smooth out the edges” goes over easy after the demo.

In reality I end up with 50-75% time to get it prod ready. The rest is QA + prod bug fixes to take it over the (practically scoped) finish line.

Re:Throw away your first draft of your code by ninepoints on Friday August 04, 2023 @05:02PM (#37005851)
This is such a HN phenomenon it infuriates me. Read article. Then read comment that misconstrues article to be something completely different, accompanied with loud critique. Read replies all in violent agreement to obviously self-evident strawman.
Re:Throw away your first draft of your code by duxup on Friday August 04, 2023 @06:21PM (#37006661)
Sadly, it’s an everywhere phenomenon :(
Re:Throw away your first draft of your code by Vicinity9635 on Friday August 04, 2023 @09:29PM (#37008053)
Back in the day on /. they would just say RTFA and spare everyone the loquacious verbosity.
Re:Throw away your first draft of your code by quickthrower2 on Saturday August 05, 2023 @12:08AM (#37008928)
It is a something-man. Maybe a strawman? Or a weakman?
Re:Throw away your first draft of your code by alsetmusic on Saturday August 05, 2023 @10:04PM (#37018262)
He-Man? Superman? Batman? Captain Planet… wait.
Re:Throw away your first draft of your code by mathisfun123 on Saturday August 05, 2023 @04:13PM (#37015893)
The misunderstanding you have is that often the article isn't the point. Just like a football match in a pub isn't always the point. People just want to chime in - the comment section is like an ephemeral pub.

If that bugs you because you consume information on the web rather than socialize, the solution is simple: don't read comment sections and make up your own mind about the article.

Re:Throw away your first draft of your code by Buttons840 on Friday August 04, 2023 @03:30PM (#37004734)
> The temptation to throw away all of your code - be it a prototype or a "grown" code base - arises often.

I want an editor plugin which allows me to mark sections of code as reviewed or "perfect" (depending on how honest I'm being). Then, when I'm tempted to rewrite everything, I can go through and mark what I think is good, and then focus on refactoring the rest until I think it is good as well.

I'm tempted to rewrite code because I lose track of what it's doing, or I've learned a lot since I wrote that old code and so I'm not sure if the old code is good anymore. It's not so much about rewriting the code as an exercise in getting familiar with the code I've already written. I want a tool to help me with this.

Re:Throw away your first draft of your code by gregmac on Friday August 04, 2023 @05:38PM (#37006239)
I would think a combination of git, unit tests, and comments would solve this problem?

Unit tests prove the code works as intended, and are basically examples of what the code is doing. Whether the code is actually "good" is a bit more subjective -- but tests give you the freedom to modify it without breaking it.

Checking into git frequently is also a way to give yourself some freedom. Commit at every milestone, like every time the next thing is "working". If you feel like refactoring, go for it -- you can reset back to working state in a few seconds.

And lastly, leave comments in. You can always clean it up before you push. You can even squash or interactively rebase your history so no one else sees the gory details how the hot dog was actually made.

Re:Throw away your first draft of your code by jon-wood on Saturday August 05, 2023 @08:30AM (#37011371)
The theory and the practice of unit tests are often different things. In theory you’ll make changes to the code, and the unit tests will show whether they had the intended effects.

In practice, because often the unit tests are mocking out all the classes they interact with (as is the recommended style for isolated tests) you’ll either have all the tests explode constantly because the interface being mocked has changed, or you’ll have tests that confidently tell you things are working despite the methods they call no longer existing.

Re:Throw away your first draft of your code by quickthrower2 on Saturday August 05, 2023 @12:12AM (#37008951)
If you could modularize code in such a way the sizable chunks could “just sit still!” then entire modules could be marked as perfect. But you wouldn’t have to since you wouldn’t naturally need to touch them (e.g. how many codebases are using a fork of core-utils, for example, … to exaggerate a conways law effect).

But unfortunately the modules don’t make themselves apparent at the start of the project. So it needs refactoring discipline.

Re:Throw away your first draft of your code by withinboredom on Friday August 04, 2023 @03:37PM (#37004819)
> Admittedly, there's nothing sexy about refactoring.

There's the "strangler fig" (my favorite) method of refactoring. It's where you rewrite just a small portion of the software, little bits at a time. Instead of doing a full rewrite.

IMHO, it's the best way to refactor. You can switch out both "old" and "new" versions at-will until you're 100% sure you've covered all the edge cases.

Re:Throw away your first draft of your code by hot_gril on Friday August 04, 2023 @03:33PM (#37004774)
I've thrown away and redone a lot of code, both mine and others', and I've never regretted it. It's always taken less time than our team would have spent dealing with the old code, plus it helps to revisit past assumptions that might've gotten stale.

Of course, I'm only doing this if I think the old code is slowing new work down too much, but I'm definitely more eager to redo things than most SWEs around me. Especially seems like they don't like rewriting their own code and will keep zombie stuff alive.

Re:Throw away your first draft of your code by m463 on Friday August 04, 2023 @03:28PM (#37004701)
It says to throw it away after a couple of DAYS, which seems to differ from other advice (like from joel-on-software)

Maybe this is ok, given that time period?

or will the rewrite have all these extra bells and whistles?

or will the rewrite throw away the unneeded bells and whistles?

Re:Throw away your first draft of your code by HenryBemis on Friday August 04, 2023 @06:06PM (#37006483)
Ok, perhaps don't throw it away. When I come up with an idea (for an app usually), I use "a few" A4-sheets (anything between 5 and 20), scribble and draw on them with a pencil, draw screens, buttons, data flows, activities, write notes (in various font sizes). Then I use my CamScanner and call this a v1.

Then email me the PDF and store the papers in a box, and a couple of days later I start the v2 (same process), then v3.

By v4 it's 'good'.

I also use the same method on my 9-5.

I take VERY seriously the Abraham Lincoln quote “Give me six hours to chop down a tree and I will spend the first four sharpening the axe” on almost everything.

I consider the v1, v2, v3 as the "sharpening the axe" and the v4 on the actual cutting.

In that spirit, the article has a similar approach, as the v1, v2, and v3 may take you down an (more than) imperfect path.

Re:Throw away your first draft of your code by balaji1 on Friday August 04, 2023 @03:16PM (#37004528)
agree it seems like Resistance, as Pressfield calls it
Re:Throw away your first draft of your code by robaato on Friday August 04, 2023 @03:46PM (#37004921)
Rather than "throw away" I would "start from scratch" meaning I can refer to prototype but reimplement with respect to appropriate norms. As a version control fanatic, very little gets really thrown away in my book. Just hidden.
Re:Throw away your first draft of your code by tnecniv on Friday August 04, 2023 @03:51PM (#37004988)
Yeah normally when I do this, it’s because I picked bad abstractions. The main business logic I can bring over almost line by line, but I want to reshape the abstractions / data structures / interface to that logic
Re:Throw away your first draft of your code by trentnix on Friday August 04, 2023 @03:51PM (#37004993)
Ah yes, starting from a clean slate.

https://devrant.com/rants/816880/i-ve-done-it-again

Re:Throw away your first draft of your code by barbariangrunge on Friday August 04, 2023 @03:57PM (#37005057)
Game companies implicitly do this, throwing out old versions of their code (sort of). You make a game. Ship it. Start on a new game. Your previous game is now sort of a practice run for making a new game from scratch. Continue, ad infinitum.

It's weird: you rarely have to maintain something for 20+ years, and you get to always improve and iterate on how you did things last time. But, are you training yourself to write hard to maintain code, since you don't really have to maintain it past a certain period? Or does the learning-from-iteration actually make writing-maintainable-code easier?

I know some people do keep developing their games for decades, look at Dwarf Fortress, I'm just talking in general.

Re:Throw away your first draft of your code by glouwbug on Friday August 04, 2023 @04:33PM (#37005506)
Minecraft is reaching that 15+ year mark
Re:Throw away your first draft of your code by hathchip on Saturday August 05, 2023 @03:44AM (#37009899)
That's the most successful game of all time, hardly a typical case.
Re:Throw away your first draft of your code by throwaway14356 on Friday August 04, 2023 @04:28PM (#37005438)
what you do you get good at. if you don't have to maintain there is no point in getting good at it or any reason to think you will gradually get better at it.
Re:Throw away your first draft of your code by wpietri on Friday August 04, 2023 @05:06PM (#37005891)
Nah. I think a prototype is something you definitionally should throw away. There's a huge freedom to knowing that instead of taking on tech debt, you're committed to declaring tech bankruptcy and throwing away the code. It lets you try things, cut corners, and generally experiment. It's great for thinking through what everybody really wants.

I do agree that it's generally a bad idea to just throw out a long-lived code base. But for me that's not an estimation issue. It's because the urge to throw it out is usually a response to upstream problems that haven't gotten fixed. For example, a lot of code bases are a mess due to time pressure. But if you don't fix the process problems that turn time pressure into mess, then you're just doing to end up with another mess. Possibly a bigger one, in that stopping all productive output in favor of a rewrite usually makes business stakeholders crazy, causing increased time pressure.

Re:Throw away your first draft of your code by duxup on Friday August 04, 2023 @06:19PM (#37006639)
Required link to Joel on Software:

https://www.joelonsoftware.com/2000/04/06/things-you-should-...

Granted Joel is taking about a wholesale rewrite of proven / established code. Code and has had had the advantage of time.

Personally, I end up rewriting parts of my code often. It usually takes a mile for me to find exactly the right way it should be based on how are people use it.

You absolutely should rewrite prototypes, and re-factor important chunks of code. I rewrote something 3 times today, it was better each time.

At the same time you should be wary…

Re:Throw away your first draft of your code by ideamotor on Saturday August 05, 2023 @01:42AM (#37009327)
Of the reasons he says engineers want to rewrite the code, he says: badly organized code, slow processing time, and ugly code.

Maybe I’m not an engineer, because I bet perfectly pretty fast code is an indicator you should reorganize it to make it easier to adapt, add more capabilities even if it runs slow, and put some ugly fixes in there so it’s useful for the end user.

Agreed that wanting to over-engineer and perfect something that should be constantly evolving is not a good reason to rewrite it. That sounds like back office bubble pretend-work.

Re:Throw away your first draft of your code by tuwtuwtuwtuw on Saturday August 05, 2023 @04:02AM (#37010000)
RTFA
Re:Throw away your first draft of your code by Tozen on Sunday August 06, 2023 @12:26AM (#37018969)
A possible alternative is to use a different programming language for doing the prototyping than the one which will be used in production. The urge to hang on to prototype code can be removed, because you are going to do a rewrite in a different language, regardless.

By using 2 different languages, there is more freedom to just make a demo, discuss, and then decide what to keep or what direction to go.