The life of a code monkey

Text

Tonight’s lesson

Sometimes the best way to express yourself is in code…. so here it goes.

Guys and girls are both people

public abstract class Person
{
   ...
}
public abstract class Male : Person, IMan
{
   ...
}
public abstract class Female : Person, IWoman
{
   ...
}

The longer one interacts with members of the opposite sex, the more you will realize that, the basic person method are all virtual. This inevitably leads to both sexes overriding the methods and providing their own implementation. Even worse sometimes the implementations don’t even invoke the base implementation.

Consider the following method within Person:

public virtual bool IsFun(Activity activity)
{
  return activity.IsFun;
}

The male implementation might look something like this:

public override bool IsFun(Activity activity)
{
  return base.IsFun(activity) && InGoodMood();
}

While the female implementation may look something like:

public override bool IsFun(Activity activity)
{
  if (IsOk() == false)
     return false;

   if (activity.EnvironmentIsPleasing(this) == false)
     return false;  

  if (activity.Participants.Select(x => Friends.Where(x => LikesFriend(x)).Contains(x)).Any())
     return true;
  
  var toInvite = Friends.Union(Classmates).Union(Family).Union(CoWorkers).Where(x => x.IsFun(activity));
  if (toInvite.Any())
     return true;
     
  return false;
}

Now before I get complaints please note that both the Male and Female classes are abstract (hence making every person unique, and allowing for more specific implementations).

Though some methods should probably never be overridden:

public virtual bool LooksGoodInDress()
{
   return this is IWoman;
}

That was tonight’s simple revelation. Men and women are different though there are times when you can be pleasantly surprised that someone of the opposite sex calls the same base method that you do.

Now for those interfaces above….

public interface IMan
{ }

public interface IWoman
{ }

Filling in these interfaces is left as an exercise for the reader.

Posted on Tuesday, January 31 2012.
Got a question or comment? Ask me.
Previous