blog

Code optimization at Jozef Chúťka's blog

October 17, 2009

1 Comment

break continue nesting optimization script

Code optimization

First, my golden rule: method with more than 12 rows is too long. Methods should be short, simple, clean and reusable. In most cases your methods fits into 5-12 rows. If not, you should consider rewriting your methods.

Look at the next code. There is too much nested tests. Soon you get lost with more and more nested levels:

function gimmeResult(a, b, c)
{
    var result;
    if(a)
    {
        if(b || c)
        {
            if(b && c < 0)
            {
                result = 1;
            }
            else
            {
                result = 2;
                // where am I?
            }
        }
        else
        {
            result = 3;
        }
    }
    else
    {
        result = 4;
    }
    return result;
}

Try to linearize code, it is much more cleaner for reading when no multiple nesting used:

function gimmeResult(a, b, c)
{
    if(!a)
        return 4;
    if(!b && !c)
        return 3;
    if(b && c < 0)
        return 1;
    return 2;
}

Sometimes your function does more things than it should. In this case, it chooses item and operate over it. But is it really good to use it this way?

function doSomething(key)
{
    for(i in list)
    {
        if(i == key)
        {
            if(item[i].value > 0)
            {
                item[i].value2 = 1;
            }
            else
            {
                item[i].value2 = 2;
            }
            if(item[i].value3 == 100)
            {
                while(item[i].value3 > 0)
                {
                    // where the hell I am now
                    item[i].value3--;
                }
            }
        }
    }
}

Split your code into separated functions. One selects your item, other operates over item. Result code is more cleaner and reusable.

function doSomething(key)
{
    item = getItem(key);
    if(!item)
        return;
    item.value2 = item.value > 0 ? 1 : 2;
    if(item.value3 != 100)
        return;
    while(item.value3 > 0)
        item.value3--;
}

function getItem(key)
{
    for(i in list)
        if(i == key)
            return list[i];
    return null;
}

Using continue in while cycle causes the current iteration to skip and go to the next one. Sometimes you do not need more processing from current iteration:

while(someIterator)
{
    // some processes here
    if(someIterator != "someValue")
    {
        // some processes here
        // and here
        // and here
    }
}

Using continue removes nesting, code is cleaner:

while(someIterator)
{
    // some processes here
    if(someIterator == "someValue")
        continue;
    // some processes here
    // and here
    // and here
}

When you do not need more iterations…

i = 100;
j = 0;
while(i)
{
    if(i > 90)
        j++;
    i--;
}
// 100 iterations means 100 "i > 50" tests

… you can use break to stop current while cycle and save unnecessary processing:

i = 100;
j = 0;
while(i)
{
    if(i <= 90)
        break;
    j++;
    i--;
}
// 10 iterations means 10 "i <= 50" tests

Where to go from here:

Myth Buster – Benchmarking Loops

Round up of ActionScript 3.0 and Flex optimization techniques and practices

Optimizing Performance for theADOBE® FLASH® PLATFORM

The Size of Empty

Faster arithmetic – Flash Assembler (not ByteCode) : And you thought it couldn’t get faster

GSkinner Talks

You may Leave a comment or Subscribe to Comments RSS or Trackback this entry.

1 comment so far

Common Tricks in ActionScript Code Shortening at Jozef Chúťka's blog March 17, 2010 14:04 […] “shortcuts”, your code may be reduced in reasonable amount of lines. Some reading about code optimization can be found here, this article extends […]

Leave a comment

Please be polite and on topic. Your e-mail will never be published.