About a month ago I finished reading the book Refactoring : Improving the Design of Exisiting Code, which is great. Coincedetally, right after finishing that, I am getting the chance to go back and refactor a project I worked on a few years ago. The fun part comes in when I can see a clear improvement in my programming, and see how much knowledge I've gained since the time I originally built the project.
For one example, looking back at this code, I can remember that I considered myself an "object-oriented programmer" simply because I knew had to make a custom class and instansiate it. I was still a ways away from really understanding key OOP concepts like encapsulation and composition.
In my original code, I wanted to create a slide show of images for a particular project, so I had this code inside a calss named "Display", which was a tangled mess of spaghetti code that tries contain the entire logic of the site in one file (picture a class with tons of switch and if/else statements and methods that would span over 100 lines):
Actionscript:
-
//load the initial jpg for the project
-
imgClip.loadMovie("images/"+clickedThumb.info.galleryName+"/"+clickedThumb.info.folder_name+"/1.jpg");
-
-
//build the sq buttons under the pic
-
var squareIconBar = buildSquareIconBar(clickedThumb.info);
-
-
//attach a progress bar for the pic that is loading
-
var progBar = ProgressBar.createProgressBar(squareIconBar, 1000, 225, 0, imgClip, broadcaster);
-
-
if (isSlideShow != true){
-
-
//stop the pre-exisiting slide show
-
stopSlideShow();
-
-
//start the timer for the images' slideshow
-
_global.startSlideshowInt = setInterval(this, "slideShowWait", 2500);
-
-
}
you can see a few things wrong with that little bit right there, namely that too much logic and too many properties necessary for controlling a slide show is contained in the Display class. After refactoring, I wound up with a new SlideShow class that contains the properties and methods (utilizing the "Move Method" refactoring), and teh code above was reduced to two lines:
Actionscript:
-
picLoader = ProjectImageSlideShow.createSlideShow(this[activeSection+"Clip"], 1001, clickedThumb.info, this);
-
-
picLoader.addEventListener("projectSlideShowComplete", Delegate.create(this, onSlideShowComplete));
Even looking at that I can see I can probably refactor taht a little more, but still I think that's pretty decent. The slide show is encapsulated, and the Disaply class just composes itself with the SLideShow object...and just listens for an event telling it that the slide show is over. There's a few more examples I would like to post...actually what I would like to do is just post the files so people can check it out if they want.
Anyway, my points are these: If you are like me and you're programming experience began with flash, take some time to learn some skills that relate to programming in general...solidify your knowledge of OOP and design patterns, and don't strictly focus on actionscript.
Also, I think it's a good thing to go back and revisit old work...it's a good way to gauge how far you've come, and to test yourself as far as what you would do differently knowing what you know now.