I am here to advocate — whenever time permits — rewriting your code from scratch. We can all look back on code that we wrote months, years, and maybe even decades ago and be thoroughly embarassed by what we produced. However, you often don’t have to wait nearly that long before reaping potential benefits from rewriting your code.

This also applies to those who take programming courses, whether self-taught or otherwise. You may complete exercises and move on, but how much of anything do you really learn when you only do it once? Go back and do the exercises one more time from scratch, and observe the difference.

Writing From Scratch

Code libraries exist that simplify a great many functions. However, it’s possible to start using these libraries without any fundamental understanding of what they’re actually doing for you. Consequently, I have observed students exhibit frustration over trying to use provided code samples beyond the given tutorials.

Perhaps I am not the only one who has observed this, as there is a growing number of code-from-scratch tutorials that help you achieve a base understanding of whatever it is you’re trying to do. By acquiring this knowledge first, you can go on to employ these libraries expertly. And while I have subscribed to this method of learning for a long time, I take it one step further.

Cooking Spaghetti Code

Often times, and others can probably relate, my goal is simply to get new code to work. Once it works and the bugs have been squished, it’s usually time to move on to the next project. A final code clean up often doesn’t happen. A problem arises, therefore, when you can begin a new project with previously-written code, and you start off with a hot mess.

Scope creep is also the scourge of many projects. Maybe you start off writing decent code, but then adding this new feature and adding that new feature quickly make a mess out of it. Your code looks like it wasn’t well thought out because it couldn’t have been.

Rewriting From Scratch

Writing from scratch, which implies learning something new, virtually guarantees writing sub-optimal code. You start off learning the basics of something, and then you hopefully learn something more advanced. Commenting out code and inserting new code is a great way to make a mess, so major improvements are a great justification for rewriting from scratch.

Rushing projects and scope creep are also great justifications. After all, you can probably tell as you’re writing the code that it’s getting ugly. For example, instead of changing a function that is already working, maybe you create a new one just so as to not break what isn’t already broken. You could end up with unnecessary duplication that way.

The Benefits of Rewriting

Redundancy is a common problem that is easier to eliminate through rewriting than through editing. Since you know what everything does, take a little time to plan out the variables and functions that you need, like you hopefully wish you could’ve done in the first place. You don’t have to wait months, years, or decades to make your code look — and probably perform — noticeably better.

Another benefit of rewriting from scratch is readability. By planning everything out, you can optimize white space and add a wealth of comments. You also end up eliminating all code that you commented out but never removed just in case it might’ve still been needed. If a project is finished and code is still commented out, it probably has no more reason to be there (I can think of exceptions).

Final Thoughts

Sometimes, I will start entire projects over from scratch. I was working on something recently, for example, when someone gave me a fantastic new idea. It was such a major improvement that it really made more sense to restart from scratch. The supporting code transferred over quickly.

It is worth noting that rewriting your code from scratch really shouldn’t take very long. No matter the size of the project, you’ve already completed it before. Plus, depending on the state of your original code, your new code might be noticeably smaller, and thus less-time-consuming to write.

Writing code once, whether for compensation or for self-improvement, can give you code that works. Rewriting it a second time from scratch can give you a sense of actual proficiency with it.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store