Here’s an excerpt of post from one of my favorite programmers/developers (creator of the game Minecraft) that caught my attention:
I’m currently working on server-side inventory, and it’s going relatively well. I’m currently pulling on hard coded strands of spaghetti code, seeing what moves, then refactoring that out into a better designed code. See, when I first wrote the inventory, it was just a test to see if it was fun, and I’ve stuck to that code ever since since I never really had to change it.
I probably would’ve saved myself some time if I had fixed the code immediately when it was still fresh in my mind.
The worst part about refactoring code is that you spend hours working on something only to have it do the exact same thing when you’re done with it. The best part is that you feel much better about working with that code in the future. It’s very cleansing, in a boring kind of way.
For those who have no idea what he is talking about, he inserts lines into the code just to see what’s needed to be added/changed or if it’s a good idea (or if it works at all), then cleans up the code by making it smoother-looking and be able to manage/change the new code easily in the future. This is known as refactoring code.
I’m not really sure how many programmers do this often, but I for one tend to do the spaghetti-code-then-refactoring when I’m not sure if it will work or if it’s a good idea. For the Flash game I’ve been making, I do this very often, especially in the beginning stages of making the game.
I did a major “restructuring” of the code because the entire (ActionScript) code was in one single file and it went past 1000 lines. Very bad programming practice when it comes to doing something very complex. The code started to become unreadable, especially when I don’t look at the code at least once a day. The reason why the code got unnecessarily long is because it started out as my own test platform for the Box2D engine, then expanded from that and got carried away.
So, the “restructuring” was really a very very long process of both refactoring code and separating the code into multiple files as classes. Now, the code is quite readable and neatly separated code so I can actually see the base structure of the code. The main file (when i finished restructuring) was reduced to 300-something lines. I was very ecstatic that the entire thing still works, even though the game itself has not changed one bit. So as Notch said, very cleansing in a boring way.
But even after the restructuring, I still do the spaghetti-code-then-refactoring process because there are so many things in the game now that adding something will interfere with an existing function. Starting out with a class is sometimes very frustrating because adding/removing/changing constructors and parameters is a lot of going back and forth. And there will be a lot of adding/removing/changing because I’m not entirely sure what parameters are needed for the classes to work properly.
Anyway, the point is, I do refactoring a lot (except in JavaScript, which is another story for another time).
Here’s an excerpt of post from one of my favorite programmers/developers (creator of
Name: J'Brian
Age: 20
This blog is my productions blog. I will mainly post whatever stuff I make that will emphasize what I specialize in.
What the blog will probably contain:
- Ideas for program, design, or product
- Drawings of ideas/concept
- Music I made (drafts and final)
- Links to websites/programs/designs/products I made or was a part of
- Work-in-progress stuff
- Unrelated but interesting (hobby-ish stuff)
Skills:
- Website design (HTML, CSS, Javascript)
- Graphic design (Photoshop and Illustrator)
- Programming (C/C++, Java, BASIC, Flash ActionScript 2/3, LUA)
- Adept at learning new programming languages
Website: http://www.rawringdesigns.net/
YouTube: http://youtube.com/jayther