The Importance of Refactoring
By: Christian Behler
Tags: Programming Tips, Programming, Software Engineering, Software Development, Refactoring

The Importance of Refactoring

Nobody likes it, Everyone should do it

After spending the past two days refactoring a major system in my game, I was reminded of the importance of refactoring code. A lot of programmers don’t like refactoring and I get it. You have a perfectly functional piece of code and you willingly break it hoping that it turns out better after you are done. It goes against the “never change a running system” mentality a lot of people have — myself included. I always get a bit anxious when I am about to refactor a bigger code section because no matter how good your plan is, there are always problems you only realize after you are halfway done with the changes. But in most cases, refactoring your code is worth it. It will be better structured, less complex, and hopefully more efficient.

Photo by Markus Spiske on Unsplash

My history with refactoring

When I first started programming I never refactored any code at all. I always preferred to start over and make the code better the second time around. I actually started over so often that it became a bit of a meme between me and a friend who was also learning programming at that time. While starting from scratch may have been a valid strategy in the beginning, as my number of libraries, classes, and lines of code grew, it became a lot less viable to do so. When you are just learning to code, refactoring isn’t an issue, because your codebase is small and you can easily keep the entire scope in your mind. Even in university hardly any project was big enough to require any real refactoring, so despite being taught the concept I only learned how to refactor once I got to work on bigger programs. At this point, working on a game and my engine, refactoring is necessary to keep the code maintainable. Obviously, perfect code doesn’t exist and there are always ways to improve the code quality, but I have been getting better at refactoring regularly.

When to refactor?

I’m sure there are people, even college professors, out there telling you to refactor after every little bit of code you’ve added. But I don’t think that’s necessarily true. In most cases, you can keep adding new features and functionality for a while before you need to think about refactoring. However, the time to refactor is a double-edged sword. If you wait too long and add too many features, the code gets messier and messier and will be a lot harder to refactor once you get around to it. If you refactor too often, your code will be very well structured and clean, but you spend more time refactoring than actually making progress. As with most things in life you need to find a balance. Especially when you are developing something completely new and you are not sure about the best way to do it you try different solutions and the code ends up being a mess. I generally focus on getting the code working correctly before I worry too much about the structure. However, I have been getting better at spending some time refactoring and polishing the code after I am done with a feature before I continue to the next, new and shiny thing, while all the details are still fresh in my head.