What is Object Oriented Programming?

In a previous blog, I said

This new code is about classes (FilePath) instead of being about functions (IsUncPath(string) ).  

Pearls of wisdom, for sure.  But what does it mean?  How do you know if you're really writing OO code?

Try this: sit down with someone (pets are fine) and explain to them how your program works.  If you find yourself saying “And then function F1 calls function F2 once for each item, which calls to F3 as needed“, then you're probably doing procedural programming.  If you say “There's this class C1, and it's responsible for blah, and it references one-or-more of class C2, which does blah.“ then you're probably doing Object Oriented Programming.

Procedural programming isn't bad, of course.  But it's pretty fashionable to do OO these last 10 years, so let's try, ok?

switch - not OO.  Can be replaced with polymorphism.  If the divergence is simple but repeats itself in many places, you can use a State or Strategy pattern.  If the divergence is more complex, then splitting up the current class may make more sense.  You can use Extract Method to get a handle on the things that should be moved around.

conditionals - not OO.  (Crazy talk, right?)  Don't go replace all your conditionals with polymorphism, with the comment “Jay said so“.  Deciding when this is the right thing to do is your job.  I'm just trying to get page hits, right?  Anyway, it's the same as switch

string - not OO.  Every string you use has assocated rules about what it can contain & how it works.  Social Security Numbers must be 9 digits, NTFS file paths can't contain '&', etc.  Make a class to hold the string and code in these rules.

all builtin types - not OO.  Same as string, but somewhat less severe.

In C#, throwing Sytem.Exception. - not OO.  Whoa, but I thought exceptions were a good OO practice?  They are, but look at how you use them.  Compare:

if (someCondition)


throw new Exception("condition wasn't met");



class SomeException : Exception


public SomeException() : base ("condition wasn't met") { }



if (someCondition)


throw new SomeException();


See how the second example says what it's doing with types?

string is so concerning, I'm going to write a separate post just for it.

Comments (8)

  1. Charles Shopsin says:

    Um, sorta. I agree with what you said, but it’s important to note (as you did a little with the conditionals bit that these aren’t rules, they are just hints. There are times when a switch is just what you need and oo would be a rediculous waste of time.

    For example, in an app I’m working on at work I need to convert a DBType to an SqlType. Now I could either write a class with 15 overloaded constructors (or methods, depending on if I have other things to do) or just write a method called ConvertToSqlType(DbType blah) with a switch inside.

    If I needed a mega convert all the dbtypes in the world to one another class, overloads might make sense, but it just has to do this one thing. It seems like having a page full of almost identical methods is kind of a waste…

    And yeah, I know that I could just set the type of an SqlParameter and then let it convert it, but that scares me because there is no explicit guarantee that it’s going to work if there is no data in there. Not to mention the fact that if you look at the code in SqlParameter (using reflector or some such tool) you’ll notice that a switch is exactly what they use.

  2. RichB says:

    For the same reason that switch is non-OO, so is enum.

  3. RichB says:

    Charles, I find that I need to use switch on values returned from components written by 3rd parties (including BCL components). This is because the 3rd party components aren’t written with the domain knowledge/use cases I require.

    But I can completely remove any code which switches on data private to my component and replace this with polymorphism.

  4. Iain says:

    I definitely agree with you on the exceptions thing, but it really annoys me that c# makes defining new exceptions harder than it could be (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp08162001.asp for how to define a fully-capable exception). This really annoys me, because it increases the amount of stuff to do in order to have a good set of exceptions. I think python shines here, you just say stuff like:

    class ThingoException(Exception):


    class ThingoDatabaseException(ThingoException):


    class ThingoDatabaseUnavailableException(ThingoDatabaseException):


    class ThingoDatabaseIntegrityException(ThingoDatabaseException):


    and then you can catch the exceptions you care about, and it takes under 5 seconds to define yourself a new exception.

    Hopefully code templates stuff in whidbey will improve this, at least to an extent.

  5. Kris says:

    Enums are definitely a source of bad smells quite often. A great refactoring is to apply "replace type with class", etc. Seeing code that acts conditionally on the enum outside of the type owning that enum member is a great indicator.

  6. Kris: Here’s that refactoring: http://www.refactoring.com/catalog/replaceTypeCodeWithClass.html

    Maybe another guideline for you is "enums should always be nested types", since you indicate the owning type is important.

    Iain: For Whidbey I’ve written a template specifically for implementing an Exception, following those guidelines. However, in my experiences I’ve never needed all the facilities I end up building that way. Maybe I’m just doing it wrong.

  7. Anonymous says:

    savagevines.com » Refactoring

  8. Yeah, that is a little off-topic. You might try asking on a C# newsgroup or a discussion board on gotdotnet.com.

Skip to main content