The Importance of Refactoring (Patreon)
Content
So, what is refactoring? Refactoring the process of taking a section of code and rewriting it so that it preforms the exact same task in a more organised and understandable way. An important thing to understand is that refactoring code should not produce any external changes in the output of the program - it is a purely internal change that makes future maintenance and expansion of the code easier.
Last night I ripped out a big chunk of the old character personality code and replaced it with a much more elegant method of calling the appropriate labels for character responses. I was able to take a couple hundred lines of code and reduce it down to about 10. The change also let me more easily test to make sure every branch of dialogue is covered by all of the different personalities in the game.
So why is refactoring important? As software projects grow they tend to accumulate poor design decisions, often simply the result of not knowing how future features are going to be implemented. As new features are added the old decisions begin to get in the way and require little hacks to make everything work properly. The more you add the more hacks you need, until even the hacks have hacks and the whole thing is a spaghetti like pile of special cases. When you refactor you take the time to correct previous bad decisions to make sure they don't force future design decisions.
Refactoring is often the programming task that is neglected because it is the only one that does not produce tangible results for the end user. A standard development cycle goes: Add a feature -> Fix bugs in feature -> Refactor code -> Repeat. Adding features and fixing bugs both produce obvious results for the user, but refactoring by itself does nothing. The advantage is that simple, well thought out code is easier to add features to and tends to produce fewer bugs while adding new features.
If you're a programmer of any skill level do yourself a favour and spend some time refactoring your code, future you will thank you for it!