Programming to an Interface

August 2, 2012


Although I’ve talked to people about this “aha! moment” of mine before, I’ve never blogged about it. Big thanks to Iris Classon for inspiring me with “365 questions” to be more open to microblogging, and to Julie Lerman for telling me about her on Twitter.

Grasping programming concepts is hard, but I think the examples people use (at least in my experience) make them even harder.

The first time someone explained the concept of a class to me, he used the friggin’ “like a blueprint of a house” example. Argh.

Similarly, inheritance was explained to me with the “shape – ball, circle, square, etc.” example (except the explanation I got did not include any reason whatsoever they needed to inherit from a base class).

I’m sure that these can be good examples if done right, but my point is, I do web sites & LOB apps. I don’t instantiate shapes, and I don’t program houses (well, except for my home automation stuff, but that’s a different topic).

To successfully grasp a programming concept, I need concrete, relatable examples.

Programming to an interface was NEVER explained well to me. Everyone always said something that resembled, “blah blah PROMISE blah blah CODE CONTRACT blah blah Win32 API blah blah OUTSIDE WORLD blah” …and they lost me, because a few years ago, I did not NEED to program Win32 APIs (or any APIs, really), and my code was not called from the “outside world.”

My Car Made Interfaces Click

When I finally gave into getting a (used) new car 2 years ago (after driving my Ford Escape for 8 years), it had features I’d never experienced before. One of them was built-in Bluetooth.

It might seem like a silly thing to you if you’ve had this feature for a long time & take it for granted, but the day I paired my cell phone with my car, I could not believe how much control it had over my phone.

I could dial numbers. I could mute my phone, I could hang up… The screen even showed me my PHONE’S CELL STRENGTH and BATTERY LEVEL! I was blown away.

How does my car know how to control my phone? It didn’t know if I was using an iPhone or a Windows phone or an Android phone. It didn’t know if the maker was HTC or Samsung.

It was at that moment that I truly understood WHY one might want to “program to an interface.”

My car has NO IDEA what I was using. All my car knew was, if it calls the method defined in the Bluetooth Spec, it should receive something in a format defined by the that spec. It didn’t care if it was pairing with my cell phone or my Win8 Slate or an alien that could telepathically send bluetooth commands through his antenna. It just sent a defined format & expected a predefined format in return.

If I push “1-800-555-5555” & hit the call button on my car, my phone takes that command & implements it however it needs to implement it. My car doesn’t know if my phone needs to open Skype, or tombstone an app, or ANY of that. That’s my phone’s OS’s job when it implements that interface.

More Great Concept-Teaching Examples

Rob Vettor uses a very relatable “coffee shop ordering system” in one of his explanations of patterns. For me, great, concrete, real-world examples are key in making concepts click. That’s the kind of thing I needed more of: “when would you do this IRL and why” examples… stuff I might actually have to do one day… that explain concepts.

If you have more examples of helpful realistic-programming-scenario explanations, please, share them in the comments!


5 Comments on “Programming to an Interface”

  1. Dan Says:

    That is a great example, one I wouldn’t have thought of on my own. I hope you don’t mind if I use it.


  2. Greg Vaughn (@gregvaughn) Says:

    Keeping with the car analogy, even the driver controls are an interface. A human learns to use the steering wheel, accelerator and brake pedal. We don’t really care whether it’s implemented by power steering or rack-and-pinion, carburetor or fuel injection, etc. Since we know how to operate the interface, we can drive any car that implements it.



  1. Dew Drop – August 3, 2012 (#1,377) | Alvin Ashcraft's Morning Dew - August 3, 2012

    […] Programming to an Interface (Cori Drew) […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: