Code in Human Terms
Mar 18, 2009 by David Brooks
I have been meaning to mention this for some time now, but something I heard Jeremy Keith say almost in passing during a conference really helped me out.
Before you start writing code, be it PHP, Ruby, JavaScript, ActionScript, etc. think about it in human terms. What do you want the code to do, and in what order should it logically progress. So rather than jumping into the code, you write it in plain language. Let’s consider a simple function to demonstrate the idea.
The Objective
We’re going to need to navigate a whole set of ActionScript movie clips and make alterations to them based on random numbers.
Steps
// Create the array of movie clips in our project
// Loop through the objects
// Pick a random number
// Move the object our random number of pixels away on the x axis
// Make the object taller by our random number
Adding the Code
// Create the array of movie clips in our project
var objectLibrary:Array = new Array(square1, square2, rectangle1, rectangle2, rectangle3, rectangle4, rectangle5);
// Loop through the objects
Var j = 0;
while (j < objectLibrary.length) {
// Pick a random number
var randomNumber = Math.floor(Math.random() * (500 - 1) + 1);
// Move the object our random number of pixels away on the x axis
objectLibrary[j].x = objectLibrary[j].x + randomNumber;
// Make the object taller by our random number
objectLibrary[j].scaleY = objectLibrary[j].height + randomNumber;
}
Obviously this code isn’t all that advanced, it’s just there as a demonstration of a thought process. Where this is really helpful is in larger scale projects that require many directions and points of interaction. I have found myself thinking that a particular task is going to be a nightmare, only to start into this process to find that it’s nothing more than a set of basic concepts stacked together. And even if you get to a step and don’t know how to achieve the result it will help you ask a very specific question (example: “How do I put a tire on my car?”). That’s better than trying to distill the concept of a project down into a question that may not have an answer (example: “How do I build a car?”).
If you’re like me, this code may help you program far beyond your actual skill level.






Post new comment