Fun with delegates
This last month, after over three years of working with .net, I finally understood what a delegate is. Now understand that I could correctly use the syntax and setup event handlers and other common uses of delegates before then. Still, I just didn't get it. Part of it is that the grammer used with event handler examples confused me. A delegate is Command *object*. In the examples, the delegate didn't seem like a thing, a noun, an object. In retrospect, I can't really say why I was confused now that I have had the light turned on for me. On the other hand, look at of the examples most of us are exposed to:
this.Button1.Click += new System.EventHandler(this.myEventHandler);
In that statement there is nothing obvious that indicates that an EventHandler is a delegate. It's defined somewhere else and appeared to me as some voodoo related to the .net event handler framework. It does look like a thing though, a noun, but somehow it reads like, “use this method as the handler for the Click Event of Button1“. Which is precisely what it does say, but it is not clear that myEventHandler has magically become an object in it's own right.
public delegate void Fireable();
OK, I'm going to go out and get me a Fireable. Sounds more like an adjective. the gun is fireable. An adjective can't really stand by itself. Now if you are designing a gun, or something that can fire an event the signature probably makes some sense:
public class Gun : Weapon, Fireable
or
public class Window : Fireable
So, a gun or a Window could be described as fireable. Now, does this make sense?
public Shooter.Shoot(Fireable fireable)
Not really, I want the thing that shoots, not a description of the thing that shoots. What I would expect is something more like:
public ShooterShoot(Weapon weapon) or maybe something like
public delegate void FireCommand();
Here is another example of something that led me from the true path:
private void OnClosing(
Object sender,
System.ComponentModel.CancelEventArgs e)
Now this is a method that will passed somewhere else as a delegate. But methods should be verbs, like Close() so on a window, a window can Close, but how does a window OnClose? A window could have an event like Closing, Opening etc. Does that look like an object? A verb? Adjective?
It was Bruce Eckel's blog about “latent typing“, as done in python, that finally turned the light on for me. In that case, he was referring to the way generics are implemented in java and c#. Internally in these implementations, the actual type is the lowly object, but externally, the implementation requires you to name, and implement an interface that the generic will be typed as. In python, it seems the only restriction to what can be added is that the signatures match. He calls that latent typing and goes on to argue that it is just as safe as the strong typing enforced by the compiler in java.
Wait just a minute, an implied type inferred by a signature, that's what a delegate is! And of course c# has this syntax coolness that in this one case allows you to refer to a method name directly instead of having to go back to reflection to get a MethodInfo, which is already an object that represents the method.
The Command pattern is a very powerful tool in object oriented design. And, better yet, it's simple enough that anyone can grasp it. And c# gives this pattern a first class implementation. Now if only I had understood this from all the examples I had seen. Seems sooo obvious now, but I wonder if I am the only one that missed the point.