This is the fifth post in the Architectural Atrocities series and now the time has come to scrutinize ActionScript 3.0. The item of discussion is interfaces and how they are used in the ActionScript 3.0 API:s.
Let me start with an OO-maxim:
If you prefix your interfaces with “I”, you have no idea how to use them
(ok, I made it up myself, but that doesn’t mean that it’s not true)
The ActionScript 3.0 API:s have a few interfaces, in general it’s a good thing, it shows that the Macromedia/Adobe developers actually understand something, which hasn’t been obvious in the past.
The decision to include interfaces in AS2 was probably a manager or marketing executive saying “hey, all the other languages have something called ‘interfaces’, why don’t we?”, and the developers quietly obeyed.
In AS3 it seems that the developers (the new ones, I presume, since it’s unlikely that those responsible for AS2 could even begin to design the AS3 API:s) have a general understanding of object oriented software development and what interfaces are and why they are usable.
The point of this rambling
An interface declares a type. It’s an abstract concept and not connected to any concrete implementation. This is very usable since we can tie our code to an abstract concept and don’t worry about the actual implementation. The actual implementation can later change, but we don’t need to worry since we have only programmed to the abstract concept.
A more common OO-maxim states:
Program to an interface, not an implementation
Programming to interfaces leads to low coupling, which is a good thing for reuse and maintenance. If you don’t belive me, you’re disqualified from making a comment to this post.
The problem with the AS3 API:s is that every single interface I have found is prefixed with “I”. I assume this is in order to make it blindingly obvious that it’s an interface*. Given the explanation above, can you see the problem?
The point finally
If an interface is a type, and you want to program to interfaces, your ActionScript code will be littered with types that start with “I”.
Types are types. It doesn’t matter if they are abstract or concrete, they are types, you shouldn’t distinguish between a type defined by an interface and one defined by an imlementation (a class).
Prefixing with “I” takes interfaces aside and tells us that these are special cases, not abstract types. I read code like this as “This is an IFootballPlayer”, not “this is a football player”. This is wrong, a type represents a concept, not a type name and naming conventions should make it easier for the client programmer to think in this way, not harder.
To prefix interfaces with “I” is to not understand that types declared in interfaces are the types you will use in your code. Classes are just implementation specifics. Interfaces are not a convenience for emulating multiple inheritance, and they are not just a way to guarantee that a bunch of classes have the same methods.
* This is sometimes called Hungarian notation, you usually see it in newbie programmers that haven’t started to belive in the holy type checker.