Welcome to Honest Illusion Sign in | Join | Help

Let's talk about some code (Javascript)

Ya'know, I'd always intended this blog to be one of them cool ".Net Blogs", with just a occasional entry about my life.  Ok, so two years later, I've got the occasion random life entry thing down, but in all the time, not one thing about programming.  Let's try correcting that.

Lately, I've been reading  The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks, which is otherwise a fine book on Javscript technique. (website)  However, every now & then, the authors just get a bit caught up in "clever" code.  For example, this snippet from pg. 178:

function dofade(steps, img, value, targetvisibility, otype)
{
  value+=(targetvisibility ? 1 : -1) / steps;
  if (targetvisibility ? value > 1 : value < 0)
        value = targetvisibility ? 1 : 0;
 setfade(img, value, otype);
 if (targetvisibility ? value < 1 : value > 0)
 {
    setTimeout(function()
    {
      dofade(step, img, value, targetvisibility, otype);
    }, 1000/ fps);
  }
}

Now, the important thing to note here, is that nearly every line is affected by the value of targetvisibility, and we test for that value 4 times in just that short function.  Now, let's consider if we just tested for it once:

function dofade(steps, img, value, targetvisibility, otype)
{
  if (targetvisibility)
  {
    value += 1 / steps;
    if (value > 1)
        value = 1;
    setfade(img, value, otype);
    if (value < 1)
    {
     setTimeout(function()
     {
       dofade(step, img, value, true, otype);
     }, 1000/ fps);
    }
  }
  else
  {
    value += -1 / steps;
    if (value < 0)
        value = 0;
    setfade(img, value, otype);
    if (value > 0)
    {
     setTimeout(function()
     {
      dofade(step, img, value, false, otype);
     }, 1000/ fps);
    }
  }
}

So, now while, it's a bit longer, it's a whole bunch easier to read & understand. But let's take this a step further.  How do we initially call this function?  Again, from pg. 178:

if (dir == 'out') {dofade(steps, img, 1, false, otype);}
else {dofade(steps, img, 1, true, otype);}

The significance of this is that now, nowhere is the targetvisibility parameter not set explicitly. In which case, there's no need for it:

if (dir == 'out') {dofadeOut(steps, img, 1, otype);}
else {dofadeIn(steps, img, 1, otype);}

function dofadeIn(steps, img, value, otype)
  {
    value += 1 / steps;
    if (value > 1)
        value = 1;
    setfade(img, value, otype);
    if (value < 1)
    {
     setTimeout(function()
     {
       dofadeIn(step, img, value, otype);
     }, 1000/ fps);
    }
  }

function dofadeOut(steps, img, value, otype)
  {
    value += -1 / steps;
    if (value < 0)
        value = 0;
    setfade(img, value, otype);
    if (value > 0)
    {
     setTimeout(function()
     {
      dofadeOut(steps, img, value, otype);
     }, 1000/ fps);
    }
  }

 

Share this post: Email it! | bookmark it! | digg it! | reddit!
Readability Stats: Word Count: 648; Sentence Count: 17; Grade Level: 10.0, more info...
Published Tuesday, May 23, 2006 11:57 AM by James
Filed under: , ,

Comments

No Comments
New Comments to this post are disabled