Wednesday, December 1, 2010

Arrow code

As the name suggests code that look like an arrow fall under the catagory of the Arrow Code.

These are caused when data structures contain data structure, and there is a need to run concentric loops, on them.

e.g.

for( int i = 0; i < some_integer; i++ ) {
    if( i == PRESET ) {
        for( int j = 0; j < some_integer; j++ ) {
            if( j == PRESET ) {
                for( int k = 0; k < some_integer; k++ ) {
                    //and so on.
                }
            }
        }
    }
}

The dazzling effects of the code are for everyone to "see".

I had come across this very pattern in my code. The root cause was that i was passing by value, and creating unwanted duplicates.

I changed everything to pointers and there was just 1 for loop and a condition check. Which was pretty cool.

So the next time you see the "arrow code", be pretty sure there is some unwanted checks going on.