The journal of David Brooks, designer, developer and photographer.

Code in Human Terms

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

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Recent Articles

Latest Comments

Northward Compass

Colophon

© 2005 - 2010 David Brooks, all rights reserved.