Thursday, July 22, 2010

Cognitive Dissonance, or why I don't like this picture

This is legal code.  The Assert completes successfully:
class Program
{
    static EventHandler eh;
    static void Main(string[] args)
    {
        int i = 0;
        eh = (source, e) => { i = 1; };
        eh.Invoke(null, EventArgs.Empty);
        System.Diagnostics.Debug.Assert(i == 1);
    }
}

As you can see, it is possible for an event handler defined elsewhere to have an anonymous method assigned to it that modifies an internal variable in the method in which it is defined. (Ignore the fact that the above example has the event handler invoked synchronously and from within the same method that defines the anonymous method.  I’ve experimented and this still works even if these things aren’t true.)

I don’t like this. The whole idea of an internal variable is that it is internal: nothing outside the method in which it is defined can access it.

Yes, the anonymous method is defined within the method, but it can be invoked from outside the method.  It isn’t internal.

I’m sure there is a very sound reason why this was done. I just can’t think of any problem that opening a tunnel to a method’s internal variables solves that couldn’t be solved some other way.

Saturday, July 03, 2010

A Thought about Organizational Systems

For me, it's more important that a system be easy to put things into than that it be easy to get things out of.

I'm willing to rummage through a fat folder of "2009 Medical Bills" to find one particular thing, as long as I'm confident that all medical bills from 2009 really are in that folder and aren't in some pile across the room. But I also know that if filing 2009 medical bills required making little piles by practitioner and labeling a new folder every time an Explanation of Benefits came in, probably the bills never got filed at all and are still in some pile across the room.

Hence: putting things into a system easily > getting things out of it in a hurry. Your mileage may vary. But if you're having trouble getting or staying organized, you might think about which side of the transaction is more important for you.