Thursday, January 31, 2008

Get to Know Your Methods

No matter how experienced you are with a language, it's a good idea to frequently browse the documents. You can find useful methods that you hadn't used before...or at least not on a daily basis or even remotely recently.

In a personal project, I've got an array of references to MovieClips and I want to sort them based on the values of four properties attached to these objects. To be honest, I haven't done much array sorting on client-side since college. Simple sorting, yes. Sorting based on more than two properties, not so much. I usually do my sorting in a database query.

So, as I started to tackle the problem I imagined looping through the array with a function that would reposition each element based on the values of their four properties. If two elements had equivalent values for a property, compare the next property. Something like the following pseudo code

for length of array
{
if ( i.p1 < i-1.p1){
move i in front of i-1
else if(i.p1 == i-1.p1){
if(i.p2 < i-1.p2){
move i in front of i-1
} else if(i.p2 == i-1.p2){
. . .
}
}
}

I could be stubborn and develop this into a full method, or I could read the docs and notice the sortOn() method of the Array Class. Which might look like this

myArray.sortOn(["p1", "p2", "p3", "p4"], Array.NUMERIC | Array.DESCENDING);

which does the job quite nicely.

Wednesday, January 30, 2008

No Wrong Ways

It always bothers me when people say that there's a wrong way to code. As far as I'm concerned, there are no wrong ways, just ways and much much better ways.

Poetic Code and Avoiding Duplicate Variable Definition Error

While talking with a non-coder friend , I mentioned that my muse is a night owl and usually visits late at night. He was surprised, and said "you mean there's a programming muse?"

Oh yes, there is a programming muse! Some people call it "being in the zone". When my muse visits, it's as if the code is writing itself... and doing it well...with meaningful comments!

My friend was skeptical, and said poets have muses. My instant response was that code can be a lot like poetry, albeit a strict form of poetry. It can certainly be elegant, to a reader who understands it's form and meaning.

He remained skeptical, but the conversation planted a seed in the back of my brain. Since then, I've noticed a pattern in my coding preferences. I crave poetic symmetry and pattern. When I have to break pattern to avoid some compiler errors, I'm uncomfortable.

The following code snippet, will not generate compiler errors (in context), but it has an ugliness to it.

Now I know this is just a matter of personal taste, but it would look more symmetrical if I could use "var" in each clause as if the clause was a function and declare that variable in each clause. That way, I could keep "var" in front of each variable. To me, dropping "var" for repeats (like p1 and p2), disrupts the rhythm of the code for me.


if(gen == 1){

// two peaks
var p1 = A.x - T.hz1;
var p2 = A.x + T.hz1;

peaks.push(p1, p2);

} else if(gen == 2){

// 4 peaks
p1 = A.x - T.hz2;
p2 = A.x + T.hz2;
var p3 = A.x + 2*T.hz1 - T.hz2;
var p4 = p3 + 2*T.hz2;

peaks.push(p1, p2, p3, p4);

} else if(gen == 3){

//8 peaks
p1 = A.x - T.hz3;
p2 = A.x + T.hz3;
p3 = A.x + 2*T.hz2 - T.hz3;
p4 = p3 + 2*T.hz3;
var p5 = A.x + 2*T.hz1 - T.hz3;
var p6 = p5 + 2*T.hz3;
var p7 = p5 + 2*T.hz2;
var p8 = p7 + 2*T.hz3;

peaks.push(p1, p2, p3, p4, p5, p6, p7, p8);

}

Update: In context, the code above is called in sequence, so that the first time it's run gen=1 and the next time gen=2.

Tuesday, January 29, 2008

First Post in New Blog

Why have I started this blog? I'm thinking of dropping off the radar in the Flash community for a while. I feel the need to shed the baggage of my identity, and write as though I don't have an audience. It's not that I'm writing into the void, as I have given the link to a few friends that I think might be interested. I'm not writing anonymously, I'm just writing more "quietly".

What will I be writing about? Mostly Flash and my own learning experience. I have to admit that I'm not a very good programmer when it comes to "not re-inventing the wheel". I usually try to figure things out for myself, instead of following established patterns and algorithms. But I'm not completely stubborn, I do make use of them.

Why did I call it "Underground Flash"? Well, my aim is to write one geek to another, without any commercial expectations. That doesn't mean that I'll never have ad banners on the page...since it would be nice to have a little extra pocket change...but that I'm not writing to a commercial perspective. I want to talk about the artistry of writing code, as well as fumbling with getting things to just work.

Am I really going to keep this up? Well...we'll see. Hopefully, I will write more than I have been.