Another annoying mistake (ActionScript 3.0)

Debugging is not always a torture. It is when we have to beat a deadline and when eliminating bugs from a new product is matter of life and death, but when we have enough time and we are enthusiastic enough, then every new bug is an opportunity to learn something important and useful. Mistakes are essential part of going forward, that’s why we have to enjoy it. Some esoteric masters tell that “perfection is not an end but a journey to become better” which is a bit corny but there is some truth in that. (And it remembers me of the quote “each man, one way; each horse; one stance; each church; one Buddha… each master to his own technique”.)

Today I’ve been debugged an error for hours in my current AS3 project, because I had an array that remained empty, and I didn’t know, why. Those functions, that were responsible for the construction of the content of the array, were similar to the following short function:

...

function createArrayOfPersons(n: int) : Array {
  var retval : Array = new Array();
  for (var iii : int = 0; iii < n; iii++) {
    retval.push(new Array());
    Array(retval[iii]).push("John Doe");
    Array(retval[iii]).push(0);
    Array(retval[iii]).push("unknown");
  }
  return retval;
}

...

I was a bit clueless, I didn’t understand what the problem is, and then I had an epiphany. It is the well-known issue of the dangerous static cast that can be problematic also in C++. Static cast tend to create new, temporary objects, so if you perform operations on the result of a static cast, then you typically change just a temporary copy and the original object doesn’t change at all.

I don’t know, why, but I always try to be as clear as possible when I’m coding, and I like to write more when it helps to express my ideas in a more clear way. For example, in C++, when I create a new object, I prefer the style Something s1 = Something(10); instead of Something s1(10);. (Both of them call the same conversion constructor, but 1.) the longer version is easier to understand 2.) it helps to avoid the issue with the Something s1();, because Something s1 = Something(); works well. The longer and shorter style do the same thing even if we call the copy constructor in these ways.) I tend to be a big mouth, but being a big mouth is not always good, the problem of static cast shows that.

One can note, that the code above is pretty ugly. When we use two functions and we don’t write to write everything in one function, then the whole issue disappear:

...

function createNewRecordOfPerson() : Array {
  var retval : Array = new Array();
  retval.push("John Doe");
  retval.push(0);
  retval.push("unknown");
  return retval;
}

function createArrayOfPersons(n: int) : Array {
  var retval : Array = new Array();
  for (var iii : int = 0; iii < n; iii++)
    retval.push(createNewRecordOfPerson());
  return retval;
}

...

In this case, the compiler always knows the exact type of the modified object. That’s why they say that good coding style helps to avoid problems, but of course it cannot replace thinking.

Advertisements

About vrichard86

I'm Richard and I'm an enthusiastic coder. I want to be better and I think a professional journal is a great self improvement tool. Welcome to my blog, to my hq, to my home on the web!
This entry was posted in AS3 and tagged , , , . Bookmark the permalink.