Jake Pruitt

This is where I write.

← Home

Refactor

I felt gross and guilty as I opened up my old Gruntfile.js to refactor the build process. I held my breath as I jumped into the pool of old code that I wrote weeks ago. I wrote it hoping I would never have to step foot in it again, and it reminded me of clearing out dust that had been swept under the rug for months.

This article is about the principle of refactoring, a core principle that drives the community of developers everywhere. Whether you are a beginner or seasoned veteran, it is important to understand the importance of refactoring.

The Code is Bad

One of my favorite writers in the English language is Joseph Conrad. The amazing thing about Joseph Conrad is that he did not learn English until he was 21. This fact always comforts me when I learn to code, because if Joseph Conrad could learn how to write The Heart of Darkness and Lord Jim in a foreign language, then I can learn to write great JavaScript, a language that is just as foreign and expressive as English.

Joseph Conrad wrote in the preface to one of his best novels:

A work that aspires, however humbly, to the condition of art should carry its justification in every line.

I often think about this when writing JavaScript. The beauty of a program comes from its ability to use exactly what it needs, and to be justifiable at every single line. This is not an easy feat, and often code becomes ugly when a lot of it is thrown together, and more is thrown on top of it to fix the old ugly code, but it just makes it uglier.

In order to fix this kind of code, you need to constantly return to old code and question every line, and reconsider the implementation from a fresh, more experienced perspective. The code may work, but that does not make it good. Bad code still works, but good code will be easier to understand in the long run, and may have marked performance improvements.

A Commitment to Craft

As a developer, you are responsible for making your code as solid as possible. This process is very difficult, and it can be very embarassing to have code sent back to you from your team, saying that it is not good enough, or that it needs to be refactored. But just remember that it is not you that is wrong, it is just the code. And the code can be rewritten to be better.

F. Scott Fitzgerald was known for meticulously editing the Great Gatsby. Over the course of years, he rewrote significant parts of the novel. “What I cut out of it both physically and emotionally,” he wrote, “would make another novel!” (Introduction to Modern Library edilion of The Great Gatsby (New York, 1934))

It does not take years of revision to make a good program. It does take a passion for revision and a continual drive to improve. You must doubt and question every line of code that you thought was perfect at one time. This questioning makes a good developer, and the best developers can tell the difference between that good code and code that is bad even while they are writing it.

To get to that point, you have to never let your code become stale. The first way to becoming a great developer is to write great code, and to rewrite it into better code.