The Importance of Refactoring
Added 2018-07-22 06:18:32 +0000 UTCLast night I sat down and did one of the least interesting but most important things you can do in software development - I refactored a section of code. Since I like writing little essays about the different tasks involved in the development of Lab Rats I thought I would write about what refactoring is and why it's so important (and so often overlooked).
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!
Comments
I'll do my best to chuck comments into my code to help people out who are looking through it. It doesn't even cross my mind sometimes that people are going to be looking at it, but I'm glad some people are cracking it open and experimenting!
Vren
2018-07-25 03:10:53 +0000 UTCYeah, I'll admit to being a little guilty of not making use of proper testing principles as much as I should. LR2 is getting to the point where it's complicated enough that it needs it though, so I'm going to be making some effort to set up tests over the next few updates.
Vren
2018-07-25 03:09:19 +0000 UTCRefactoring, if it makes your code much clearer or shorter, can be very satsifying. Like cleaning out your room or getting 4 lines on tetris.
2018-07-24 21:44:33 +0000 UTCRegression testing: Vren, one thing I end up falling back on is regression testing. If I'm writing a block of code that is modular, I sit down and generate scripts that will pound on all of the inputs and make sure that I get all of the output I'm expecting. The nice thing about having a regression test is that when I refactor, I can be sure I have not actually changed anything in the API. If I think the code is solid, I can just run the regression test, and if it passes, I'm golden.
2018-07-23 18:33:39 +0000 UTCI work on a project that has hundreds of thousands of lines of code. Refactoring is a way of life. There are programmers that just sort of assume they excrete gold, and never look back. And there are the fellas and gals that go back over stuff, just out of principle, and are not afraid to say "There must be a better way to do this!" I always enjoy running into a fellow member of that later group!
2018-07-23 18:24:30 +0000 UTCThe simple fact that you take the time needed to refactor the code makes the 5$ worth it, remember though that less code doesn't necessarily means fast code, and faster code is also of essence :)
waffel
2018-07-22 15:06:09 +0000 UTCI love posts like these, giving an insight on game development. Also we could always have more comments for the codes!
2018-07-22 07:22:54 +0000 UTCHeh. Also see Gates' law for further reading.
Mr. L
2018-07-22 06:59:50 +0000 UTC