Types Make Life Easier
I recently read a blog post by Gilad Bracha, I know, late to the party, but here’s said post: http://gbracha.blogspot.com/2011/06/types-are-anti-modular.html
I whole-heartedly disagree with him, and I will try to explain why. I am a huge fan of type-casting, it is one of the reasons I enjoy writing AS3 a lot more than I enjoy writing JavaScript. Even if the statement “Types Are Anti-Modular” were true (it’s not), there are way more pros to type-casting than that one con:
a) Type casting makes things make sense.
At any point in a project you can map out all dependencies, see which modules interact with each other and essentially, you could, with only parsing through code, print out a meaningful diagram of your entire application.
We’re talking Hansel and Gretel here. What happened when the birds ate the breadcrumbs? They got lost. No type-casting, means lost developers.
Type-casting gives you structure, meaningful tangible structure. Especially important if you are working on a project with more than one developer involved.
b) Type casting saves development time.
Anybody that has written code with any level of IntelliSense knows how much easier it can be to code with a type-casted language. I write code so much faster and efficiently in AS3 because I can type out 3 or 4 letters of a given method, rather than all 15 characters. Not only that, it significantly cuts down on the frequency of typoing your variables or method calls.
Hitting CTRL+ SPACE gives you an easy way to reference all variables and methods on a given type of object. This is huge, especially when working with Classes that were written by another developer, or Classes that you wrote a while back that are no longer fresh in memory. In JavaScript, with no type-casting, I not only have to track down the file said Class is declared in, but I have to scroll through hundreds of lines of code just to see what variables and functions exist in said Class, and if I want to know what functionA or functionB return, I have to read through the entire function to determine a return type.
c) Type casting polices proper structure.
With type-casted languages you get early warnings of, “No, you’re doing this all wrong!” By expecting classes of IFruit, if a developer tries to pass a non IFruit type object to a method expecting an object of IFruit, it will throw a fit, even if the method does not use methods explicity available to IFruit type Objects. Such as Obj.eat(), which could exist on objects inheriting IMeat, IVeggie, IPerson.
b) Types are not anti-modular
Rather, the problem lies in the structure of a given application, or more specifically the problem lies in the developer(s) who don’t know how to properly structure an application.
Interfaces exist for a reason, there is no reason to compile ObjectA-ObjectZ into your module, instead you use interfaces. Interfaces should be created, not for every Object, but for groups of Objects, i.e. Apple, Banana, Orange, Mango all inherit from IFruit, and your only dependency in the Smoothie Class, is IFruit, you don’t depend on Apple, Banana, Orange, Mango, etc… The footprint is low, for a lot of gain.
Interfaces act as blueprints, and really they are well worth the effort to be able to benefit from the rest of the pros of type-casting.
Interfaces, when used properly totally negate the “Types are Anti-Modular” argument.
