Friday, May 27, 2016

The Great Code Cleanup


     Got real sick of how annoying debugging can be and decided to clean up my code. I organized everything so that I finally know what each line does. With a system like this I can hunt for things to modify much more quickly instead of playing Where's Waldo every time I look for a variable.

     It took a week longer than I thought it would, but it's worth it. The lost time can pay for itself in how quickly things can be implemented now that code controls have been rearranged in uniform patterns, the comments are standardized, I'm being much more generous with spacing, and the code is laid out in a way that guides the eye to where I need to focus to solve a specific problem or squeeze in new features.




     This is actually the image I saw on Facebook that got me thinking about how I write my code and why. Someone in the comments mentioned how people used to compress things into as few lines as possible in the old days because screen resolution was a big problem. The only reason it might be seen today is because old habits die hard. But I did it because I thought fewer lines decreased the file size and fewer lines meant getting right to the point and finding things easily.

     Now I know that when you have tons of layered if/else lines, brackets, and parenthesis on the same one or two lines, you end up with an unreadable mess where you run into a lot of bugs based on confused logic that made sense on paper. At least a quarter of my problems in the past have been due to typos rather than wrong logic.

     I gave each line its space and grouped similar things together, so now any typo or missing connection will stand out like a fire truck in a swimming pool.

     Another issue I had was with changing the way I ordered functions or left comments from script-to-script. I'd spend an unnecessary amount of time scrolling through them just to find the function I know is there but can't find due to it blending in with everything else, or being lost somewhere in the middle of the file instead of the beginning or end where I'd expect it to be.

     Commenting with "/*" headers in one script while using it for subdividers in another. All bad practices I corrected now. From here on out I have standardized ways of describing each line and diving scripts up into nice, readable chapters. I even figured out how to color-code headers to make them stand out more and be visible when scrolling by super fast.



     At the end of it all, I came out on the other side this week with a game foundation that will be much easier to build on and maintain. I can't tell you how much time I used to waste just starting at my clumped together pieces of code just trying to find where to start looking for the problem to debug or even just remember what the heck something did.

     My future self will be thankful for how much easier this makes programming in my future games. This moment of long overdue enlightenment completely changes the way I see programming. I feel like I can learn any language and apply these organizational techniques to make sense of any syntax. Finishing Battle Gem Ponies feels more feasible than ever.

No comments:

Post a Comment