When and why would you generally sacrifice typesafety for a nicer programming interface?
Let me give you an example: if you had the choice between two event aggregators, which one would you prefer and why?
Reflective Version:
SomeEvent.subscribe(instance, "nameOfAMethod"); //method called via reflection
SomeEvent.fire(arg1, arg2); //the firing could actually even be statically typed
Statically typed version:
EventSystem.getEvent(SomeEvent.class).subscribe(new EventHandler<Payload>() {
public void eventOccurred(Object sender, Payload payload) {
//event handler code here
}
});
EventSystem.getEvent(SomeEvent.class).fireEvent(payload);
Please note, that in Java, due to type erasure, you cannot implement a generic interface with different type parameters more than once and need to resort to anonymous or external classes for handlers.
Now the reflective event system has a nicer user interface, but you lose type safety. Which one would you prefer? Would you create empty event classes just for the sake of having a symbol, like Microsoft does it with PRISM in its event aggregator?
1
When and why would you generally sacrifice typesafety for a nicer programming interface?
Rarely if ever, and then only if the type un-safety was dealt with effectively in another manner.
Verbose/awkward programming interfaces yield a little fewer bugs, and far lower severity bugs than typeless interfaces in the middle of typed languages. Because let’s be honest, using string lookups or casting out of Object
is its own horrible programming interface – now you have a horrible programming interface and typing issues.
3
I think your example does not really compare things at the same level of detail, whereas this would:
-
stringly typed version:
public void subscribeTo(Event event) { event.substribe(this, "handleEvent"); } public void handleEvent(Object sender, Payload payload) { //do something }
-
statically typed version:
public void subscribeTo(Event event) { event.substribe(new EventHandler<Payload>() { public void eventOccurred(Object sender, Payload payload) { //do something } }); }
So in essence the latter is not critically longer. But as others have pointed out, it is both faster and safer.
If you’re really that worried about token count, you shouldn’t be using Java to start with 😉
You would be sacrificing more than just type safety. Reflection is at least an order of magnitude slower in Java than using strong types.
It’s a false dichotomy anyway. It’s possible to have a nice programming API without sacrificing type safety. See http://boo.codehaus.org/
3
I actually implemented a strongly-typed event system similar to yours once (in C#). It was horribly over-engineered and horrible to use. By the time I had discovered how horrible it was, it had spread throughout my application like a virus. It took forever to refactor, and it felt good when it was gone.
Would you create empty event classes just for the sake of having a symbol, like Microsoft does it with PRISM in its event aggregator?
Like this?
applicationEvents.Foo.Subscribe(myHandler)
Yes. Definitely yes. Why not? It’s way more simple, easy to understand, and type safe to boot. Any new developer would have all the available events in one place and wouldn’t have to go searching your source code to figure out what events are available and how to use them.
No sense in sacrificing type safety when you don’t have to.
EDIT:
If I seem overly aggressive, I apologize. I don’t mean to criticize your design, I am just lamenting my own mistake which I made in the past, and it’s very similar to what you’re doing. Just be careful not to overcomplicate things.
if you have the time now then do it with the type checking. if its code that might last up to December 2013 you will regret method 1 later, unless its a small project and you will be the only one working on it … no even then if you have the time i would go with the typed.
Why? clearer, less mistakes, more verbose yes but will help keep some bugs at bay and that is a good thing
I am not 100% certain what those two examples are supposed to do, but I guess an alternative would be to use an enum. I imagine the number of allowed strings like "nameOfAMethod"
is limited and you could put the anonymous class gory stuff in that enum.
enum Methods {
NAME_OF_A_METHOD {
EventHandler get(final Object instance) {
return new EventHandler<Payload>() {
public void eventOccurred(Object sender, Payload payload) {
//do something with instance
}
}
}
};
abstract EventHandler get(Object instance);
}
And your example could become:
EventSystem.getEvent(SomeEvent.class).subscribe(NAME_OF_A_METHOD.get(instance));
Not sure if it is directly applicable to your use case but you get the idea.