Realities of Dynamic Typing: ActionScript
Join the DZone community and get the full member experience.Join For Free
this time example comes from adobe, developer of flex and actionscript programming ria language for their flash runtime. this interface represents a simple ajax-style callback for an asynchronous operation (full source code):
* this interface provides the contract for any service
* that needs to respond to remote or asynchronous calls.
public interface iresponder
* this method is called by a service when the return value
* has been received.
datais typed as object, it is often
* (but not always) an mx.rpc.events.resultevent.
* this method is called by a service when an error has been received.
infois typed as object it is often
* (but not always) an mx.rpc.events.faultevent.
now, looks innocent isn’t. but look carefully into the comments… let me put in bold the offending logic there:
is typed as object it is often (but not always) an...
now, is it not great :) i don’t know what prevented adobe to have a strongly typed contract here - backward compatibility or the case of premature design for extension.
proponents of this style will argue the following:
- the developer almost always knows what type to expect so the manual cast or dynamic method call is fine and usually safe
- you should always write unit tests to test for the expected type
again, i don’t know what is more absurd is the fact that you now need to write unit tests for the compiler logic (how else i should say that?) or the fact that you, the developer, is expected now to remember what types to use where… (and don’t forget to keep your comments in the code up to date with expected type information – or else!). and, of course, you have no idea what exceptions this code should through in case of any errors occurred in the callback logic itself.
let me give you a counter example from gridgain . here is the method signature from grid interface that allows you to execute the grid task having the class of that task (it will be auto-deployed if it wasn’t deployed already):
public <t, r> gridtaskfuture<r> execute(class<? extends gridtask<t, r>> taskcls, @nullable t arg)
this signature provides wells of type safety with just a few extra characters in the signature saving your potentially hundreds of lines in unit testing and headaches of crashing application in runtime. here are the contracts that are automatically enforced by the compiler and supported by type-safe refactoring:
- grid task expects two parameters of types <t> and <r> - type of the task’s argument and type of the task’s result value
- method execute(…) returns task future that will contain result value of type <r>
- execute(…) method accepts execution argument of type <t>
- execute(…) method accepts any subclass of gridtask<t, r> interface
- method execute() throw gridexception to indicate error conditions and the called must provide handling logic
- parameter arg is nullable.
to understand the benefits of this approach try to imagine amount of unit tests and comments you will have to write to maintain the same type invariant… strongly typed approach truly saves massive amount of work and time. think about it…
Published at DZone with permission of Nikita Ivanov, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.