Wednesday, May 30, 2007

StackOverflow

I am pretty sure a lot of programmers encountered the StackOverflow error.
To be frank, in my early programming adventures, i never thought too much about the stack. 'Just another theory thing', i thought. But then, i learned my lessons.

Now, there ain't a lot of ways to cause stack overflows, but if you are really feeling itchy, the following mistake will certainly create it.


public void methodB()
{
methodA();
}

-- Call one of these methods --
public void methodA()
{
methodB();
}
But usually when i create stackoverflows, its usually because i call such recursive methods which are residing in different classes. So be careful, u never know when you create such a situation in the jungle of code.

Saturday, May 26, 2007

if statements

Method 1:

if(isTrue())
doSomething()


<--- identical --->
Method 2:

if(isTrue())
{
doSomething()
}

Me being the bad programmer, found myself debugging for a looonnnnggg time over a very stupid mistake made from the above convenience.

I used the first method somewhere in the thick jungle of my code. After some weeks, i decided to add some operation that would do another thing before the doSomething() function, along with many other changes.

As you might have guessed, i forgot the braces, and what happens next is an hour of scratching head and tearing of hair. :)
I felt like giving myself a tight slap when i found out why my code did not work as expected.