Endless Obsession

better code

The Exo-suite (and client scripts on Github)

Off and on at work over the last year or so I have been working on an open-source project called ExoWeb, as well as supporting projects called ExoGraph and ExoRule. ExoWeb and ExoGraph are hosted on Codeplex, though they are somewhat stale at this point.

Before I go any farther I’ll give you (and future self) my take on the purpose of these projects.

ExoGraph

…is essentially an enabler.

From the codeplex site:

ExoGraph is a graph library that leverages type information to optimize graph operations and notifications. The library exposes type-level events for object initialization, property retrieval, property path changes, and custom domain events within a graph context.

This doesn’t sound very useful in and of itself, right? Well, it’s not supposed to be. It’s an enabler, remember? That’s where ExoWeb and ExoRule come in…

ExoRule

…is a rules framework that leverages ExoGraph. That is all for now.

ExoWeb

…is a JavaScript framework, basically. It also relies on a supporting server-side web request handler as well as a working ExoGraph implementation.

From the codeplex site, ExoWeb…

…aims to provide a rich JavaScript object model, intuitive UI code based on the fundamental languages of the web (HTML, CSS, and JavaScript), model- and UI-driven validation, and seamless synchronization of changes between client and server.

That may not be crystal-clear, but fortunately you can browse the source, which is what I really want to write about anyway.

The JavaScript source is now hosted on Github. If you take a look at the source on Codeplex you’ll notice that there are 8 script files: exoweb.js, exoweb.model.js, exoweb.mapper.js, exoweb.view.js, exoweb.ui.js, exoweb.mock.js, exoweb.jquery.js, and start.debug.js. The source on Github, however, has many more script files as well as specs (using jasmine) and a build process (using rake) that produces the scripts listed above. It is loosely modeled after the jquery source.

I have a few goals in hosting the project on Github.

  1. Isolate classes and related functions in order to make it easier to manage, illuminate code structure, improve architecture and design, and limit dependencies.
  2. Allow for (hopefully) isolated testing of individual classes and functions.
  3. Isolate the bulk of the code which is not dependent on any particular JavaScript library or server-side technology.
  4. Take advantage of github’s popularity in open source software collaboration, and…well, I just like Github.

At the time of this writing the project produces the destination scripts that more or less match the scripts in the original project (not the ones on codeplex, they’re old). Also, a few of the source files have corresponding specs that pass, using nodejs to run them.

Finally, in writing the build script, I got the chance to learn a little rake (ruby make), which I highly recommend.

One of my other goals is to create working ExoGraph/ExoWeb implementations for django and rails. The beginnings of the django implementation is currently hosted on Github as well.

Installing VirtualBox Guest Additions

First, a disclaimer: you may not find this post useful unless you are interested in virtualbox or virtualization. Also, there are plenty of other blogs that will tell you how to install the guest additions. This is my personal reference.

According to the virtualbox wiki, virtualbox is the only professional-quality virtualization solution that is also Open Source Software. Alright, a big +1 for open source! Not that I have ever cracked open the source… :P

Another big plus for me is that it is cross-platform. I use windows and linux machines, so this is very important to me.

As an aside, VMware player appears to also be somewhat cross-platform (windows and linux, no OSX). I have used it as well, but I feel better about my experience with virtualbox. However, I don’t have any real solid reasons to explain why. Feel free to try to change my mind if you must…

So what are the guest additions? From the wiki, it is special software that can be installed inside Windows, Linux and Solaris virtual machines to improve performance and make integration much more seamless. Things like mouse pointer integration and arbitrary screen solutions (e.g. by resizing the guest window). That’s the kicker for me. I install them in my linux guests so that the guest resolution will adjust to match the host window.

Anyway, the point of this is how to actually install them. Here is a link to a good set of instructions in case you don’t find these sufficient.

First, mount the guest additions “disc” from the host window’s menu: Devices > Install Guest Additions.

Mount Guest Additions Menu

Now you should see the disc mounted in nautilus. The next step is to copy the appropriate file to your home folder (or some other location on the guest machine).

$ cp /media//VBoxLinuxAdditions-x86.run .

Now, make the file runnable.

$ sudo chmod +x VBoxLinuxAdditions-x86.run

Finally, run the installer.

$ sudo ./VBoxLinuxAdditions-x86.run

That’s all!

Resize jQuery Dialog

If you ever need to resize and reposition a jQuery dialog, the following script should do the trick:

function setDialogSize($dialogEl, width, height) {
    $dialogEl.dialog("option", "width", width);
    $dialogEl.dialog("option", "height", height);
    $dialogEl.dialog("option", "position", $dialogEl.dialog("option", "position"));
}

If you change a UI component’s option it should respond by updating immediately to reflect the change. If it doesn’t, it’s a bug. I read that somewhere :)

IE "Unknown runtime error"

I received a wonderful error from Internet Explorer today: “Unknown runtime error”.

It seemed to be an Internet Explorer issue only, since it worked fine in other browsers. I was able to track this down to where the innerHTML for an element was being set.

I found out here that the problem was because my HTML was invalid. As the poster notes, IE is actually enforcing the spec, which is good. However, the error message is completely worthless, which is not good. Sadly, this is what I expect from IE.

In his example, he was attempting to inject an “li” into a “p”, which is obviously invalid. He noted that IE’s behavior seemed to be inconsistent, since it was ok with an “ul”. However, in my case I found that injecting an “ul” into a “p” caused the same problem. This may have been changed recently, a la Windows Update…

Getting Things Done: Smart Lists

Smart Lists

In my first gtd post, I described some of the strategies that I employ to help me get things done. Remember the Milk has become a increasingly large part of that. If you’re reading this and you don’t know about it, you should check it out!

Anyway, one thing that I discovered recently that has changed the way I use RTM is smart lists. Seriously, I’m kicking myself for not knowing about this already! Basically, smart lists are a way to save a search for later use, or, for the techies, a stored query.

This may not seem like a big deal, but for me it is HUGE. This means that I can organize my tasks logically without having to worry about the different views that I might want to simulate.

To give you an idea of how this helps, here is my list of current smart lists (subject to change…a lot):

  • Big Projects: Things that are going to take a long time. These don’t normally show up on my day-to-day activity. It allows me to get an idea of high-level things that I’m working towards. There also shouldn’t be a lot of this due at the same time.
  • Blog: Uses a “blog” tag so that I can track blogging stuff in separate logical lists (work, personal, etc…)
  • Important: Priority 1 tasks from all lists. This list should stay pretty sparse.
  • No Due: Items that have not been assigned a due date are probably tentative or I may need to get rid of them.
  • No Estimate: Since some of my lists take time estimates in to account, it pays for me to move tasks out of this list.
  • No Priority: Much like no due, these tasks may need to be cleaned up.
  • Quick and Easy: Time estimate is low. This is ordered by due date, so I can use it to pick up easy tasks when I have a little time to spare.
  • This Week: Everything that is due this week, except for today.
  • Today: Self explanatory
  • Work Day: This is the same as “Today”, except filtered by an “am” tag that I use to indicate things that should be or can be done during the work day. Walking the dog is not one of these, if I had a dog that is…

I have now switched to using the “Work Day” list only for “I Forgot the Milk” on my work computer. This way, I don’t have to mentally filter out tasks that I can’t do until I get home.

I have yet to realize the full potential of smart lists, but I know that they will make things run a lot smoother for me.

Sequenced SQL Records

How to sequence values in a table. The non-so-elegant solution…

Imagine you have a table that contains records with a foreign key (string, int, guid, etc…) and an index or sequence number, 1-3, so that there are three records for each foreign key value. In another table you have any number of records that contain the same foreign key, as well as a value field (date, number, string, etc…) that can be sequenced in some logical fashion corresponding to the sequence 1-3 in the key table. You mission (should you choose to accept it) is to get the values from the value table and join them to the corresponding sequence records in the key table.

What follows is the answer I arrived at late this afternoon. Please let me know if you have a better way.

declare @dates table(num varchar(9), date datetime)

insert into @dates
select '192837465', cast('1/3/2008' as datetime) union
select '192837465', cast('3/12/2003' as datetime) union
select '192837465', cast('5/22/2010' as datetime) union
select '192837465', cast('2/1/2004' as datetime)

declare @keys table(num varchar(9), sequence int)

insert into @keys
select '192837465', 1 union
select '192837465', 2 union
select '192837465', 3

select
    k.num,
    k.sequence,
    date = (
        select MIN(t.date)
        from (
            select top 1 date
            from @dates
            where num = k.num
            order by date desc
        ) t
    )
from @keys k
where sequence = 1

union

select
    k.num,
    k.sequence,
    date = (
        select MIN(t.date)
        from (
            select top 2 date
            from @dates
            where num = k.num
            order by date desc
        ) t
    )
from @keys k
where sequence = 2

union

select
    k.num,
    k.sequence,
    date = (
        select MIN(t.date)
        from (
            select top 3 date
            from @dates
            where num = k.num
            order by date desc
        ) t
    )
from @keys k
where sequence = 3

I union the key table 3 times, filtering by each sequence number. In each sub-query, the value is obtained by selecting the top N values, filtered by the foreign key and ordered by their value (descending), and then getting the min value.

Kind of an odd-ball case, but I might find this humorous one day. And Pete will laugh, because I laughed at my code.

Getting things done (GTD)

Lately I’ve become somewhat obsessed with getting things done. I wasn’t always this way, I swear! In my younger days I was as disorganized and lazy as anyone. And I procrastinated sometimes (understatement of the year).

For me, getting things done has to be more than a state of mind or goal, it has to be a system and a habit. I didn’t come to this conclusion all at once. It happened gradually, and I’m always trying to be better. The system looks something like this:

  • Always write things down (requires discipline & habit)
  • Make sure its in your face or readily available
  • Use a check list system to provide positive reinforcement
  • Figure out how everything works together and stick with it

I think I was supposed to do something…

It started with the realization that I can be extremely forgetful. At least, I think thats how it started… So, the natural response to this realization was to start writing down EVERYTHING, with the understanding that if it isn’t written down I won’t remember it and, therefore, there is zero chance that it will be done. That’s a little extreme, but sometimes it helps to think about things in terms of the worst-case scenario. I started out using GMail, Google tasks, Google calendar, and, eventually, remember the milk.

I’m going to jump ahead of myself and talk about ways to make sure you always write things down. In the beginning I would write things on sheets of paper and even on my hand and arm at times. This is convenient, but its not the most reliable system. I use RTM pretty heavily at this point, so it gets a lot of my attention when it comes to efficiency.

The first thing I did was make sure that RTM could be launched as a desktop app using the shortcut launching software of my choice (launchy). I accomplished this using Prism, which allows you to use firefox to convert a website into a desktop application. This is important to me for two reason: isolation and convenience. First, a website like RTM is different than most in that I don’t need to be connected to the rest of the web and I don’t need to arrive there from anywhere else. In essence, it is less like a traditional website and more like a standalone application. Second, I would like to be able to have RTM open and close in the blink of an eye. Currently I can type Win+Space (to open launchy), then rtm (or any other query that finds my prism app), then Enter, and remember the milk is open pretty much immediately. Then, thanks to RTM keyboard shortcuts, I can type in a new task (or do pretty much anything else) and my hands never have to leave the keyboard! If I could get Vimium to play nice with RTM things would be even better!

What I have been looking at more recently is the ability to add a task directly from launchy. What makes this possible is the fact that remember the milk supports adding tasks via email. There was a solution for this that uses twitter, but I have heard that it isn’t very reliable. I would rather not go back to writing notes on my hand and hoping that I don’t need to wash them. More recently there have been a couple of solutions that make use of the email API instead (lifehacker and sheenonline). I’m currently using a modified version of the second option, which I will try to finish up and post soon.

Because of all of this, most basic task management can be accomplished with very little impact on my normal workflow.

Break the Procrastination Habit

Well, that’s great progress, but its not the end of the story. The second realization was that if I write things down I won’t forget them, but that doesn’t mean I’ll actually do them. I’m still lazy and I like to procrastinate. I had defeated forgetfulness, now I set out on a glorious crusade against the forces of procrastination. My first order of businesses was to make sure that the “things” were always before me, which would (in theory) increase the odds that they actually “get done”.

I started out by adding RTM to my GMail inbox (the gadget, not the browser add-on) and my Google calendar. Of course, this is only useful as long as I’m using GMail or my calendar. Since I use windows 7 at work, I installed the “I Forgot the Milk“ desktop gadget, which gives you basically the same view as the GMail gadget. With the windows 7 sidebar gadget, I can be sure that my tasks are always visible on my desktop. There is also a handy little app that allows you to run the same interface as a desktop app, but I don’t have any use for it currently.

The Task List

Now that I’m using RTM, I need to figure out how to use its many feature effectively. Here is a collection of notes about how I use RTM. I will modify this as I refine the process.

List

I started out with only a couple of lists, but the number has grown. The reason for this is that more lists means fewer tasks in each list, which means less tasks that I never see (and forget about) because they extend beyond the browser window. Logical grouping is the main factor when deciding on lists, but a low task count is a close second.

Priorities

The tasks in RTM can be assigned a priority of 1-3 (or none). This is useful because it determines the order of tasks in RTM and related apps. I have bounced around between only assigning priorities to some tasks, then assigning to all tasks, then back to only some. Recently I have started using the following rule-of-thumb to assigning priorities, rather than arbitrary choices. I must resist the urge to make everything priority 1 :)

  1. High Priority: These tasks absolutely have to be done. You don’t want to forget about them, and your life will be adversely affected if you fail to complete. If there is a due date assigned, it is probably not an optional due date (ex: pay rent, schedule dentist appointment).
  2. Medium Priority: These tasks are also important, but life will go on if they are not done. If a due date is assigned it should be done by then, but is probably somewhat flexible (ex: important phone call, write blog post).
  3. Low Priority: These are less important tasks that don’t necessarily have to be done, but you have decided that you want to, as time permits. These usually won’t have due dates, except in the case that it is something that happens at a certain date/time but is still optional, or something that you do at a regular interval (ex: watch world cup final, clean bathroom sink).
  4. No Priority: These are tasks that are things that you _might _want to do, but haven’t decided the if or when.

Due Date

I have been in the habit of assigning due dates to tasks and then postponing them repeatedly. Kind of depressing…

There are two ways that I use due date: when something is due (really), and when I want to do something. Differentiating between the two is something that I have not been able to figure out (suggestions welcome). Because of the way that I use gadgets I think I am pretty much tied to using due date for both real due dates and pseudo due dates.

Repeat

I use this field to set things like daily, weekly, and monthly chores. Pretty intuitive.

So far I have not used most of the other fields since they do not affect my workflow.

What’s Next?

For a while now I have been using RTM almost exclusively. This has worked pretty well so far, but is not perfect. The next step is to figure out how GMail, Remember the Milk, and Google Calendar can work together.

DataContractSerializer Read-Only Property Serialization Error

I recently encountered an exception with a message that looked something like this:

There was an error deserializing the object of type Foo. No set method for property ‘’ in type ‘’.

Huh?

This was coming out of WriteObject (or a similar method) of DataContractSerializer (or DataContractJsonSerializer, or some other flavor).

If you see this error, don’t be thrown off by the bizarre error message. If my experience holds true this error is in fact caused by a read-only property. Unfortunately, the error message won’t give you any clues to what the property is, and the offending type is probably NOT the type Foo, as the error message suggests.

Description of my scenario:

  • Attempt to serialize an object with a property that is a list of WCF serializable objects (base type).
  • The last item in the list is a sub type, “Foo”, that contains a property WITHOUT a setter. Serialization is ok.
  • After the item of type “Foo”, another item of type “Bar” (a different sub type) is added. This type does NOT have any properties without a setter. Attempting to serialize the parent object now fails with message “There was an error deserializing the object of type Bar, My Assembly. No set method for property ‘’ in type ‘’.”

See this ticket and this stackoverflow post (author describes a slightly different scenario) for more information.

Good luck! :)

Extension Methods and Generic Type Parameters

Edit 2010-06-29: question on stackoverflow

Edit 2010-07-01: question answered

This is going to be a short post, and I’ll get right to the point.

I’m looking at ways to improve the consistency, brevity, and readability of some code in the application I’m working on. The starting code looked something like this:

context.GetGraphType<Bar>().Subscribe<Fizz>(
  (instance, evt) => e.Execute((Bar)instance.Instance)
);

It breaks down like this:

  • Get some type
  • Subscribe to an event
  • When the event happens, do something

It just so happens that we have all followed an unspoken convention: an Execute method with a single parameter that is of the type that is subscribing to the event (Bar). You can imagine that once you have over a dozen of these subscriptions it can start to look a little silly. More on that later.

So, I wanted to rewrite it to look something like this:

typeof(Bar).SubscribeTo<Fizz>(context);

My hope was that it now reads something like “bar subscribes to the fizz event on the given context”, rather than “the context gets the bar type and subscribes to fizz and then does some stuff.” Hopefully I’m not totally off base here. I did check with a co-worker and he confirmed my thoughts.

In order to accomplish the above I wanted to make use of an abstract generic base class for the events, something like:

public abstract class Event<T>
{
    public abstract void Execute(T arg);
}

In the example, T is Bar - the type that subscribes to the event. I implemented the SubscribeTo method as a local extension method. Unfortunately, I got stuck when I tried to set up a generic type parameter that would be of type Bar. Something like this:

public static void SubscribeTo<TEvent>(this Type type, Context context)
    where TEvent : Event<the value of parameter 1>
{
    // do stuff...
}

If this were possible, one would be able to implement the change I proposed with type checking. The compiler would verify that Fizz was a subclass of Event and the extension method could treat it as such.

Do you know of a way to implement what I have proposed, or an alternative that is very similar?

Back to the convention I mentioned earlier. In the spirit of utilizing conventions, I ended up using reflection to take advantage of it. It isn’t checked by the compiler, but we have the next best thing. The code runs on application start-up and the Execute method is retrieved when the event is subscribed to, not raised. So, it will fail very early if there is a problem.

arguments...callee...caller...

Originally posted May 3 2010

Recently I spent some time looking at generating a stack trace in JavaScript for the purpose of error reporting. There are plenty of existing projects out there that solve this problem, so I wouldn’t recommend rolling your own.

As a result of this I came across some odd behavior in JavaScript. The code I was working with takes advantage of the special “arguments” variable (the “array” of arguments passed to the current function), which has a special “callee” property (the function that was called), which also has a special “caller” property (the function that called this function). You can probably see how this would be tempting if you’re trying to construct a stack trace. Unfortunately, this information is attached to the function object rather than some sort of “function invocation” object. There are at least two cases where this makes constructing an accurate stack trace impossible.

  1. Function recursively calls itself.
    • Since the caller property is attached to the function itself there is no way of knowing (through this information alone) how many times the function was called.
  2. Function A calls function B, which then calls function A…
    • Even if there are terminating condition, the code that attempts to reconstruct the stack trace (using “callee” and “caller”) will not terminate. There is a good description of this behavior here.

I was primarily interested in scenario 2, since this can cause errors. To get around this you can detect that you have already encountered a given function and simply stop. Of course this means that you lose any information that follows, but at least you won’t hang the browser.

Conclusion: always check MDC :)

http://eriwen.com/javascript/stacktrace-update/

http://helephant.com/2007/05/diy-javascript-stack-trace/

http://bytes.com/topic/javascript/answers/470251-recursive-functions-arguments-callee-caller

http://msdn.microsoft.com/en-us/library/7t96kt3h

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function/caller