Archive for October, 2008

Using an interface vs. a base class

Friday, October 17th, 2008

One of the concepts that confuse newbies to object-oriented programming is the difference between using an interface and using an abstract base class for one's objects. Admittedly, it is fairly confusing to have these two distinct forms of polymorphism: and too many people learn it in very abstract terms.

So it wasn't a surprise that this question cropped up in Stack Overflow:

When should I use an interface and when should I use a base class?

Should it always be an interface if I don't want to actually define a base implementation of the methods?

If I have a Dog and Cat class. Why would I want to implement IPet instead of PetBase? I can understand having interfaces for ISheds or IBarks (IMakesNoise?), because those can be placed on a pet by pet basis, but I don't understand which to use for a generic Pet.

I loved the analogy the asker had, so I took it upon myself to take advantage of the Dog/Cat analogy to present my reply:

Both a dog and a cat are animals, specifically, quadruped mammals (animals are waaay too general). Let us assume that you have an abstract class Mammal, for both of them:

    public abstract class Mammal

This base class will probably have default methods such as:

  • Hunt
  • Feed
  • Mate

All of which are behavior that have more or less the same implementation between either species. To define this you will have:

    public class Dog : Mammal
    public class Cat : Mammal

Now let's suppose there are other mammals, which we will usually see in a zoo:

    public class Giraffe : Mammal
    public class Rhinoceros : Mammal
    public class Hippopotamus : Mammal

This will still be valid because at the core of the functionality, Hunt(), Feed() and Mate() will still be the same.

However, giraffes, rhinoceros, and hippos are not exactly animals that you can make pets out of. That's where an interface will be useful:

    public interface IPettable
    {
        IList<Trick> Tricks{get; set;}
        void Bathe();
        void Train(Trick t);
    }

The implementation for the above contract will not be the same between a cat and dog; putting their implementations in an abstract class to inherit will be a bad idea.

Your Dog and Cat definitions should now look like:

    public class Dog : Mammal, IPettable
    public class Cat : Mammal, IPettable

Theoretically you can override them from a higher base class, but essentially an interface allows you to add on only the things you need unto a class without the need for inheritance.

Consequently, because you can usually only inherit from one abstract class (in most statically typed OO languages that is... exceptions include C++) but be able to implement multiple interfaces, it allows you to construct objects in a strictly as required basis.

Google launches Map Maker

Wednesday, October 8th, 2008

Google MapmakerNearly a year ago I posted my gripe regarding the inaccuracy of Google Maps road data for Manila, voicing out my concern and hoping Google will do something about the situation.

Last night, Google announced the answer to my prayers: earlier Google formally launched Google Map Maker here in the Philippines, allowing ordinary users to populate map data which will later be pushed into Google Maps.

While the service has yet to be reflected in Google Maps, the map data has so far been promising, and is a far cry from the map data that is currently available in Google Maps:

Bad Google Maps data
Existing Google Maps road data. Click on pic to enlarge


Google Map Maker data so far. Click on pic to enlarge

The data in these maps are not yet perfect: there are roads which have inaccurate names, and a lot of location data for banks, gas stations, schools and the like are not yet there. Also, while Metro Manila street data is fairly extensive, there are still some gaping holes and missing roads in several areas. Rural road data likewise tends to be more sparse.

If you have intimate knowledge of provincial roads and city streets as well as establishments outside of NCR, or even just of the streets and establishments around your neighborhood, Google Map Maker needs you to make the maps more accurate, informative, and consequently, useful. You would only need to have a Google account to sign in.

Visit Google Map Maker at http://www.google.com/mapmaker.

Interviews: Getting to know what you don’t know

Thursday, October 2nd, 2008

Three years ago, I got into an interview into a company that was looking to hire a .NET developer for the first time. It was mainly a Java software house and, until that point, they have not had a requirement that called for a developer who mainly worked on the Microsoft stack.

Having had worked on .NET for a year and a half with both Windows forms and ASP.NET development up my sleeve, I was pretty confident that I knew enough to be able to get the job. That is, until the interviewer presented me with a problem that appeared pretty basic at the onset: write a function that would get the factorial of a given positive integer.

I wrote mine with an ordinary for loop. I was pretty sure it worked. Until the interviewer continued.

"Now rewrite that using recursion."

I froze. "What?"

"Use recursion." And right then and there I didn't know what to do. Now, some of you might think that recursion is so elementary that anybody with the amount of experience I had at the time would've known it, but obviously, I did not. I hadn't graduated with a Computer Science or Computer Engineering or Software Engineering degree: I was mainly a hobby developer until I took it seriously and tried my hand at the software development industry.

I was able to muddle through with most of it (thanks to Google for the most part) but until that day, I hadn't heard of what later appeared to me as a very basic software development concept. I was floored.

Fast forward to last week. This time I was trying out for a position with Microsoft itself; there were openings for their Shanghai and Copenhagen offices, and a hiring team from Redmond was sent to Manila. I easily passed the first two screening interviews, so I was pretty sure I'd make it to the last one.

In came the first interviewer with the first question: find if a given string is a palindrome, e.g., it's the same spelling whether spelled forwards or backwards. So I gave the following answer:

 
    public bool IsPalindrome(string s)
    {
        return String.Reverse(s) == s;
    }
 

Guess what: that answer is wrong. I tried to find out why, but my interviewer didn't care to expound and showed me the door.

Ironically, on the evening of that interview, Stack Overflow Podcast 23 came out and Joel Spolsky tells a story of how he was shocked to find out that using strlen() to find the length of a string degrades the algorithmic performance of a certain piece of code, when his code was reviewed by a senior developer. Turns out I had precisely the same problem: reversing the string adds to the complexity of my solution because it needed to traverse the whole string again. For a few thousand characters it would be perfectly fine, but for a string with millions or trillions of characters (e.g., a DNA sequence) it would've been very, very slow.

And I didn't know that considering that I've been on the industry for six years already.

Despite not getting the offer, I am nonetheless thankful for being able to experience a Microsoft interview. It merely affirms the lesson I learned back in the "recursion" interview: interviews are a great way to assess your abilities, and to find out what else you need to learn.

In a profession that thrives on learning new things, this information is quite invaluable.