Tuesday, December 06, 2005

the global trap

The nice thing with script languages is that you can do quick powerful hacks to solve problems. The ugly thing with script languages is that you can do quick powerful hacks to solve problems. Whenever you do so with a problem that is not trivial you reliably walk into one of the many traps script languages provide for you. Today I walked again into the global trap. This trap is constructed by the fact that by default every variable is global in many script languages thus if you forget to declare all variables local in a function you are likely to overwrite another variable with the same name in a somewhere completely unrelated place of your script. Some script languages even don't allow you to declare variables local.

This is not the only trap script languages provide for you to fall into. Another one for example is that variables are typically not typesafe and there are myriad numbers of operators that "magically" cast variables to arbitrary appropriate types. Although this seems to be a quite convenient feature and some designers of script languages are even proud that their operators "attempt to do what you mean", on a second thought this is one of the nastiest traps these languages prepare for you. The problem is that even when a operator in most cases does "what you mean" there are some cases where it does not. And because of the fact that the operator "attempts to do what you mean" in a huge number of special cases it is typically such complex that most programmers don't really understand the rules that control the way the operator works and thus they cannot understand bugs that result from a case where their intention differs from the meaning of the operator.

So be prepared to run in these or many similar traps when you go the script way you pretend the most easy one. And if your project is a bit more than a trivial one you _will_ run into these traps even if you consider yourself a guru programmer.

No comments: