|
Re: Polymorphism without Planning
|
Posted: Feb 3, 2005 8:19 AM
|
|
Although this technique is useful for retro-fitting and for aspect-oriented programming, it certainly does not practically help in reducing the memory footprint of an application. The reason is simple: polymorphic objects usually live on the heap. The presented example is the most trivial case using stack objects.
If I want to have, let's say, 40 polymorphic objects stored in a collection using interface references, there is exactly no difference in the memory usage: it may be that 4 bytes are spared from the 40 objects, but I have another 40 interface reference objects with 4 bytes each that reference the original 40 objects.
Another point is that when I use interface reference types, I break the contract between the classes: another programmer, unaware of needing to use a specific class through interface references, might come along and use the non-polymorphic classes without interface references, and thus break the interface contract. This is a serious problem, especially in large programs.
Then there is also the problem of maintaining the interfaces: when a method that is part of an interface changes changes in some class, then there are some big problems:
1) the interfaces need to be maintained. 2) if the changed method is overloaded, the compiler would happily accept the overloaded method, possibly introducing subtle bugs. The problem is even bigger if the interface itself has overloaded methods.
Finally, a compiler might optimize vtables better than interface references (I am not sure about that, though).
So, from a software engineering point of view, using interface references is bad: if there is an interface, then it must be obeyed by all subscribers at all times. When the interface changes, it must automatically be reflected to all subscribers to the interface. Since there is practically no memory benefit and certainly no execution speed benefit, BIL sounds like a software engineering method that is asking for trouble.
|
|