Readable Conditionals

// NOTE: this code won't do anything in you're timline....

// do you do this?

if (ball.x < 0 || ball.y < 0 || ball.x > stage.stageWidth || ball.y > stage.stageHeight){
       // cause ball to explode
}

// or this?

if (isAtStageEdge(ball)){
       // cause ball to explode
}

function isAtStageEdge(mc:MovieClip):Boolean{
   return (mc.x < 0 || mc.y < 0 || mc.x > stage.stageWidth || mc.y > stage.stageHeight);
}

This is pretty standard stuff, but it can increase code readability especially for complex conditionals.

I also do this if I find myself repeating an a semi-complex if statement in two or more places.

If I’m just brainstorming I probably won’t bother, but on real projects it helps keep code readable.

Leave a Reply

Your email address will not be published. Required fields are marked *