Simplify:  Use Boolean Expressions Creatively

ActionScript 2.0

I was coding up a slideshow this afternoon for a quick demonstration to a client.  In the end, much of today’s “rough draft” ActionScript will be converted into a custom SlideManager class.  For the time being, though, my “jump in and throw something together” approach was helpful anyway, because it brought to mind a number of features the client is going to ask about — I’d bet money on it — and now I’ll be prepared.  In addition, it reminded me of an admirable principle in programming:  elegance. 

This entry is more of a “think about it in broad terms” tip than the usual “type this ActionScript, exactly.”  Here’s the situation.  I had just finished writing a set of functions to advance the slideshow.  They were called previous() and next().  I had already stored a number of slide objects in an array (these will eventually be instances of a Slide class).  The first thing the next() function does is check if the current slide is less than the total number of slides.  If it is, then the slideshow hasn’t yet reached its end, which means the “next” button can remain active.  If it isn’t, then the slideshow has reached its end, and the “next” button needs to be disabled.  This includes disabling any event handlers associated with it, dimming it (graying it out), and a handful of other things.  At first, I handled this with an if statement.  Makes sense, right?

function next():Void {
  if (currentSlide < slides.length – 1) {
    // still going
  } else {
    // reached the end; disable
  }
}

The if statement checks the value of a currentSlide variable against the value of the Array.length property a particular slides array.  The part at the end that substracts one is there because arrays start at zero, rather than one.  In an array of three items, the array’s length is 3, but its elements run from 0 through 2.

I had already written a class called NavigationButton, which features an active property.  I coded active to be similar in principle to MovieClip.enabled or Button.enabled:  set it to true and the button is active; set it to false, it disables, automatically dims, and does the other things it needs to.  So that if statement above could have been written like this (and, in fact, was).

function next():Void {
  if (currentSlide < slides.length – 1) {
    btnNext.active = true;
  } else {
    btnNext.active = false;
  }
}

Still makes perfect sense.  When the slideshow is over, deactivate the button; otherwise, leave it active.  But then I saw the correlation between the true/false nature of the if statement and the true/false nature of the active property.  Suddenly, I saw where I could collapse those lines into something neater, more elegant.  I rewrote it like this:

function next():Void {
  btnNext.active = (currentSlide < slides.length - 1);
}

See why that works?  The expression currentSlide < slides.length – 1 hasn’t changed.  It’s purpose is to get that if statement to make a decision, either true or false, which is all an if statement ever does anyway.  That expression resolves to either true or false, and since the NavigationButton.active property takes a Boolean value, the expression can be used to actually set the property, rather than steer an if statement toward doing the same thing.

NSurveyor, who often comments on this blog, uses this sort of “trick” more than anyone I know, often to great effect.  When it reduces the number of lines you have to type, it’s a terrific practice.  Of course, it also has the potential to make code more difficult to read, especially to someone who wasn’t present when you wrote it, so weigh the pros and cons before going nuts with it.  But it sure it neat to “fold” several lines into one!

5 Responses to “Simplify:  Use Boolean Expressions Creatively”

  1. Paul Mayne Says:

    Very cool. Thanks for sharing. I’m dropping this into my little file of code snippets.

  2. NSurveyor Says:

    Also, you can take advantage of the fact that casting a boolean (true and false) as a number gives you 1 and 0, respectively. So if you wanted to play frame X if a boolean were false, or otherwise play frame Y (if the boolean were true), you could use something like:

    var gotoFrame = X+Number(bool)*(Y-X);
    this.gotoAndStop(gotoFrame);

    To see why it works, consider when bool = false: Number(bool) would equal 0, and then you would have X+0*(Y-X) = X. Next, consider when bool = true: Number(bool) would equal 1, and then you would have X+1*(Y-X) = X+Y-X=Y.

  3. Tiemen Says:

    Marvellous, NSurveyor :)

  4. Jay Says:

    I have a slideshow that loads external .jpgs with a preloader in between. The drawback with my code is that each picture is reloaded into one movieclip, so one pic fades out then the next fades in once it has loaded.

    A better solution would be for the next pic to fade in over the top of prev pic (i.e. 2 movieclips instead of one.
    I’ve check through my reference books and can’t find an example of this so i was wondering if the boolean function was used for this to divert the fade in fade out functions between 2 movieclips.

    Anyone got any ideas?

  5. David Stiller Says:

    Jay,

    What you’ve described would definitely require two movie clips, but the logic involved could be accomplished any number of ways. In other words, the actual mechanics of fading one clip over another doesn’t hinge on a creative or clever use of Boolean expressions.

Leave a Reply