BalletA co-worker gave a presentation on a project we worked on a long time ago and he asked me to update an feature in the web application. The web app was originally programmed in ASP and I had almost forgotten how painful it was to work in that environment. The new request was to enable live presence information in a web part using the new RTC libraries. Fortunately, another developer had a component to perform all of the calls using the RTC libraries, but I needed to retrieve the group members for the selected group and cross-reference the group members with online presence.

The actual details involved opening "MTSAdmin.Catalog.1" object and looking for a specific COM+ package, enumerating the "RolesInPackage", and then getting the "UsersInRole". There were several layers of filtering going on and I wanted an elegant solution. Unfortunately I kept trying to be too elegant and I was getting hung up on how to make it more elegant. I wanted to have a "before", "item", and "after" callback functions at each of the function depth levels where the "item" function would occur in the iteration loop. This would allow for scenarios where items might need to be collected after iteration, particularly to avoid making multiple unnecessary calls to the lower filtering methods.

Small example from jQuery

ajax: function( s ) {
  // Extend the settings, but re-extend 's' so that it can be
  // checked again later (in the test suite, specifically)
  s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));

  // ... snip, snip, snip ...

  // Allow custom headers/mimetypes
  if ( s.beforeSend && s.beforeSend(xhr, s) === false ) {
    // cleanup active request counter
    s.global && jQuery.active--;
    // close opended socket
    xhr.abort();
    return false;
  }

The example above from jQuery only uses the "before" concept that I mentioned above, but it shows a very nice implementation where the 's' object is based on jQuery.ajaxSettings. Just before the AJAX call is about to be made, jQuery checks for any custom headers and calls the 'beforeSend' function if it exists.

Start Simple and Add Progressive Elegance

BaseballHomerun I did manage to get the function working correctly, but I had to take a step back and start simple and progressively build additional functionality into the component. Sometimes you have inspiration and you can just design and code and everything is great. Other times it can take a while before the inspiration comes. It is better to start coding and have something you can refine rather than wait and lose valuable time.

To use a baseball analogy: Don't try to hit a homerun every time; sometimes you need to play "small ball" and get people in scoring position and just try to make contact. A similar soccer (futból!) analogy would be don't just try to dribble down the field and score every time, but you need to pass and set up the players around you to maximize the scoring opportunity.