Readable Conditionals

Actionscript:
  1. // NOTE: this code won't do anything in you're timline....
  2.  
  3. // do you do this?
  4.  
  5. if (ball.x <0 || ball.y <0 || ball.x> stage.stageWidth || ball.y> stage.stageHeight){
  6.        // cause ball to explode
  7. }
  8.  
  9. // or this?
  10.  
  11. if (isAtStageEdge(ball)){
  12.        // cause ball to explode
  13. }
  14.  
  15. function isAtStageEdge(mc:MovieClip):Boolean{
  16.    return (mc.x <0 || mc.y <0 || mc.x> stage.stageWidth || mc.y> stage.stageHeight);
  17. }

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.

This entry was posted in functions and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*