My New Tumblelog
Tuesday, February 27th, 2007Godammit, before I could get off my ass and write a tumblelog using Rails someone created exactly what I wanted in tumblelogs.
Godammit, before I could get off my ass and write a tumblelog using Rails someone created exactly what I wanted in tumblelogs.
Suppose I have an event reactor pattern set up like this:
This is all well and good as it decouples senders and receivers, blah blah blah… And in the real code these are all in separate files and all that jazz.
Anyway, something I’ve noticed many times in doing event dispatcher patterns is a tendency for these “events” to become “commands” as well… so you might have the “SaveThing” class… but then you’ll add the “ThingSavingEvent” class (which is cancellable) and the “ThingSavedEvent” class. For every “command event” you’ll have the pre- and post-events as well.
And what do they teach us about code duplication? ( =
So here’s what I wanted to do:
Here’s how it works: the CommandRegistry gets a bunch of ICommandExecuter<> instances for different commands… the SaveCommand, the ExitCommand, and so on (eventually I want to put something in the CommandRegistry to maintain order, to make the command executers transactional… but I digress). Before the CommandRegistry iterates through the executers it constructs and fires a PreCommandEvent<> of the appropriate type. Here’s a concrete example.
1. Program needs to throw the SaveCommand which implements ICommand.
2. Program calls registry.Execute(new SaveCommand());
3. Registry constructs an instance of the type PreCommandEvent
4. Registry finds list of ICommandExecuter
5. Registry constructs an instance of the type PostCommandEvent
Steps 3 – 5 are the problem here. The language does not support doing things like this:
That just doesn’t make sense. Passing a Type object in isn’t syntactically correct there… the type name is what’s expected. And not a string, either.
So, yes, I know I can do some reflection to generate the an instance of the appropriate type… and it’s not that I think reflection is hard, or anything like that… but a lot of the reflection examples depend on the run-time type of an instance of a generic type looking like this: PostCommandEvent`1[[SaveCommand,
We switched over to .NET 2.0 recently. I really dig on the new features: generics, anonymous methods, etc…
One thing caught my eye today, however: while there are many new generic-ized classes and interfaces (List < T > , IComparable < T > ) our good ol’ friend ICloneable does not have a generic counterpart. It brought me up short. Why not?
The closest thing I can come up with is this hypothetical:
public interface ICloneable < T >
{
T Clone();
}
public class Base : ICloneable < Base >
{
Base Clone() { ... }
}
public class Derived : Base
{
Base Clone() { ... }
}
Now, suppose someone wanted to stick a ICloneable < Derived > on the Derived class. Well, they couldn’t: since method overloading only uses the signature of the method (which doesn’t include the return type, only the name of the method and the parameters of the method) there would be no difference between the ICloneable < Base > Clone method and the ICloneable < Derived > Clone method. The user would have to implement two separate methods like this: ICloneable < Base > .Clone and ICloneable < derived > .Clone, which seems somehow clunky. Imagine a DerivedDerived type, saddled with another of these methods, and on and on…
I’m pretty sure of the six people who read my blog three are coders… what do you think? Does that reasoning make sense? Is this a new C# interview question?
P.S. & lt; and & gt; really suck for typing generic interfaces.
P.P.S. Moving sucks too, but, y’know, worse.
GameSpy has a neat article called Will Wright Presents Spore that documents a super-fantastic approach to game creation and gameplay.
Using deterministic, procedural algorithms in response to user-supplied data the game grows the creature and determines behavior based on its form. Awesome! I especially love the “massively single-player” aspect of downloading specifications for other players’ structures and creatures and populating the world with these buildings and beings. I cannot believe that I have to wait a year for this game to come out.
I’m also already thinking about ways I want to do this in my own as-yet-unnamed project… stay tuned. ( =
EDIT: Google video has a fantastic Spore gameplay video. 35 minutes long but so worth it.
How about a little program that uses SQL to solve a sudoku?
That’s just crazy. Crazy!
Okay, Microsoft, please please please fix this before you put Visual Studio 2005 out.
VS keeps changing project files on me and it’s screwing up my source control. We’re using StarTeam at work and, every time I build, VS changes the types of form and control files in, oh, about half the project files in the solution from “UserControl” or “Form” to “Code”... and then, arbitrarily, back again. This happens even though I haven’t done anything of the sort inside the editor itself… it just happens on solution builds. So then, when I go to StarTeam and try to check in only the files I’ve modified I have to wade through the 30 or so project files that say they’ve changed even though I have changed nothing. WTF(What the fuck)?!?!? Don’t keep erroneously changing the types of those files on solution build! Don’t make it harder for me to do my job!
So I’m reading through this Slashdot troll fest about coding and someone posts this neat article about the shuttle group’s coders at NASA and how their software is zero-defect. Surprisingly, according to the article, the group may produce what is the most expensive-per-line program in the world (420,000 lines with a $35 million dollar budget) with impressive stats like ”...[t]he last 11 versions of this software had a total of 17 errors.” I mean, wow. That’s pretty cool. I really respect the hell out of NASA, gotta admit.
Read the article. This group uses what sounds to me like a waterfall approach: lots of up-front planning, lots of documentation, rigid process… and it’s totally working for them. What does that mean for us agile folks? Are we totally wrong?
Nah. I’m very much a “tools that suit the job” kind of person instead of a “this is the best tool ever” kind of person. But it is interesting to point out when people insist that waterfall doesn’t work. Huh.