February 10, 2004

Down With Hierarchy

Perhaps you've heard this before, but I had this insight while I was taking my midterm in CS 582 over the weekend. It's about static versus dynamic typing. Ironically, in class on Monday, this is what was on the agenda.

Fundamentally, there is a great tension between static typing and code resuse. Sure, static typing gives you the compile-time assurance that your program is free of type errors, but it also limits you to writing programs that are only in a subset of all type-correct programs. This limitation poses problems.

Consider this: How can I make reusable components if I have to bind them to a particular type? A container class is a great example to illustrate this problem. Because of polymorphism, I can pick a class high enough up in the class hierarchy (a.k.a. "Object") so that virtually any class I want can be used. This only works if the languages has a singly rooted hierarchy (not C++), and it still doesn't free you of the pain of down casting everything on the way out of the container.

Java's use of interfaces is a clever approach to this problem. Instead of depending on the class hierarchy to determine validity, an interface can be used. This allows the programmer to create abstractions to handle problems that span classes. Take iterators for example. One may want to interate over many different classes, whether or not they are related. Using interfaces, this is possible. Without interfaces, this is a nightmare.

Of course, we can't forget data generics (a.k.a. templates) for some of these things too. They are particularly good for container classes, but can also be useful in place of interfaces too. I'm happy that Java 1.5 supports generics, but I hope it's not as messy to deal with as templates in C++.

Fundamentally, static typing is quite constraining. Perl is one of my favorite languages because it is dynamically typed, and I enjoy the liberty of not having typing constraints. So why do I like Java? Because interfaces and generics ease the static-typing enough while still guranteeing provable type-correctness.

Interesting stuff....

Posted by enigma at February 10, 2004 11:12 PM
Comments
Post a comment









Remember personal info?




Type in the security code: