Wednesday, October 25, 2006

I love Static Typing

I know it's a great debate: static typing vs dynamic typing. I definitely prefer statically typed languages. Lots of type errors in programs are detected during the compilation and not during the execution of programs. I think this way programs are far more reliable. It might be tedious to declare variables before using them, and specify their types, but the time you "waste" in this operation is surely repaid when you won't have to test your program thoroughly just to be sure that there are no situation where your program goes wrong because it misuses a variable. Even worse when the program silently uses a variable that is not defined.

Although I like PHP very much, I find it really irritating when your browser shows an error in your dynamic web site because you used a function that is not defined (probably just because you misspelled its name). Not to mention when your program silently misbehaves (without no sign of error) just because you used a variable that is not defined (and in that case it simply instantiates a variable with an empty value).

If a program is type-checked at compile time, it won't have to be re-checked (or at lease, completely rechecked) at each execution time (and I think you gain a lot in efficiency).

When I have to test my programs, I prefer to concentrate on the right implementation of the program logic, rather than to check whether I used the right types. That should be compiler's work.

Of course, dynamic typing is good under certain circumstances (dynamic loading of code); but for the other situations, I feel more comfortable and safe with static typing.

As you might have guessed, this post comes after some time spent on debugging a program that misbehaved just for this reason :-)

Just my opinion :-)

No comments: