Jul 27 2009

Capability Classes & Queries

Category: C Plus Plus,C#,Philosophy,Software DevelopmentWil @ 7:41 pm

ostrich_headLast week, I had the honor and pleasure of teaching an Advanced C++ class at ExxonMobil in Houston, Texas. All of the students were brilliant and were employees in their Research division. I think I was the only one there without a PhD. In fact, several of them, had multiple doctorates or post-doctorates… which I didn’t even know existed.

But I digress. Since I hadn’t taught that class in about a decade and hadn’t used C++ professionally since circa 2003, my brain pushed a lot of my C++ knowledge out into its proverbial “garage” for later retrieval. After several days of submersion in the language and C++ labs, we finally got to some clever inheritance problems and polymorphism tricks. That brought back my long, lost memory of “Capability Classes and Capability Queries”.

While not exactly rocket science, Capability Queries can be of use in C# as well as C++. So here is what a capability class looks like… well, wait, let’s lay sound ground work first:

public class Animal { }
public class WarmBloodedAnimal : Animal { }

public class Human : WarmBloodedAnimal { }
public class Bird : WarmBloodedAnimal { }

Nothing really special there is there? Both birds and a humans are warm-blooded animals. So what?!

What if we had a collection of animals… say, just for arguments sake, a List collection. Now, you would like to only have a subset of that list… in which the resultant list is warm-blooded animals that have the ability of flight. Let’s ignore the fact that we have LINQ queries for now and just focus on the subject at hand. In some random method, we could do something like this:

public void SomeRandomMethod(IList<Animal> animals_)
{
   foreach (var animal in animals_)
      if (animal is Bird)
         (animal as Bird).Fly();
}

But, what about birds that can’t fly?! Penguins can’t fly but they’re excellent swimmers. Should we add an attribute to the Animal base class for “IsCapableOfFlight”? I think not. Why pollute the interface and thusly pollute every derived class with that attribute!?!

That brings us to Capability Classes (Interfaces in C#).

public interface CapableOfFlight
{
   public void Fly();
}
public class Eagle : Bird, CapableOfFlight { }
public class Parakeet : Bird, CapableOfFlight { }
public class Ostrich : Bird { }
public class Penguin : Bird { }

public void SomeRandomMethod(IList<Animal> animals_)
{
   foreach (var animal in animals_)
      if (animal is CapableOfFlight)
         (animal as CapableOfFlight).Fly();
}

Obviously, it would be awesome to use LINQ to do a query on that collection with a ‘where’ clause that used the “animal is CapableOfFlight” but just by itself… it’s quite nice.

Go green… don’t pollute your interfaces and keep your derived/implementer classes skinny!


Jul 10 2009

Leaving Improving :(

Category: PhilosophyWil @ 2:26 pm

I came to Improving Enterprises on February 18th, 2008 and I am now officially, but sadly, leaving Improving to work a little closer to home. My time at Improving has been, in a word, awesome! I have had the blessing of working alongside the best and brightest individuals in the industry and I have learned a tremendous amount of cool new stuff as well.

I would like to thank all the Improving cool kids for the all the fun times and beer-based retrospectives. And yes, I’m including Mr. Jef_ Newsom_ in that category as well. Be sure to ask Jef about his deep-seated love of underscores sometime.
Even though I will probably see you all often, I wish you all the best in all your future endeavors!

Tags:


Jul 31 2008

Chrono-Synclastic Infundibula ©

Category: PhilosophyWil @ 11:39 am

Just imagine that your Daddy is the smartest man who ever lived on Earth, and he knows everything there is to find out, and he is exactly right about everything, and he can prove he is right about everything. Now imagine another little child on some nice world a million light-years away, and that little child’s Daddy is the smartest man who ever lived on that nice world so far away. And he is just as smart and just as right as your Daddy. Both Daddies are smart, and both Daddies are right.

Only if they ever met each other they would get into a terrible argument, because they wouldn’t agree on anything.  Now, you can say that your Daddy is right and the other little child’s Daddy is wrong, but the Universe is an awfully big place.  There is room enough for an awful lot of people to be right about things and still not agree.

The reason both Daddies can be right and still get into terrible fights is because there are so many different ways of being right.  There are places in the Universe, though, where each Daddy could finally catch on to what the other Daddy was talking about.  These places are where all the different kinds of truths fit together as nicely as the parts in your Daddy’s solar watch.  We call these places chrono-synclastic infundibula.

Chrono (KROH-no) means time.  Synclastic (sin-CLASS-tick) means curved toward the same side in all directions, like the skin of an orange.

Infundibulum (in-fun-DIB-u-lum) is what the ancient Romans like Julius Caesar and Nero called a funnel.  If you don’t know what a funnel is, get Mommy to show you one.

- Kurt Vonnegut Jr.From The Sirens of Titan, ©1988


Get Adobe Flash playerPlugin by wpburn.com wordpress themes