Clean Code Talks – Inheritance, Polymorphism, & Testing

December 8th, 2008 · 22 Comments ·

by Miško Hevery

Google Tech Talks
November 20, 2008


Is your code full of if statements? Switch statements? Do you have the same switch statement in various places? When you make changes do you find yourself making the same change to the same if/switch in several places? Did you ever forget one?

This talk will discuss approaches to using Object Oriented techniques to remove many of those conditionals. The result is cleaner, tighter, better designed code that’s easier to test, understand and maintain.



Tags: Uncategorized

22 responses so far ↓

  • Marcel Popescu // Dec 8, 2008 at 2:13 pm

    Thanks a lot for these talks… I couldn’t make it to Google (being located in Romania, Eastern Europe :D ) but I appreciate them a lot. I’m C#, not Java, but the concepts are what’s important and the languages are similar enough that I have no problem understanding the code.

    So… please keep ‘em coming :)

  • nazgob // Dec 8, 2008 at 3:26 pm

    Very good presentation. I like this series, hope it’s gonna be long :) It’ s like a sum up of Refactoring + GoF DP + Working Effectivly With Legacy Code with na nice examples. I use (or try) these stuff all the time in my work (legacy C/C++) when I refactor to fix sth or just to “Reduce the cost of change”. Long live DIP :)


  • Kai // Dec 10, 2008 at 6:49 pm

    I like your style of presentation. And this was again one I learned alot. But I am lost with the example in the presentation: could you show how to implement the toString method (with parentheses only when needed)? I could not figure it out by myself.

    Regards, Kai

  • suraj // Dec 30, 2008 at 1:13 am

    Can any one help me out ?I am working on Role base application where there are 12 role like Admin,manager,Reporter,…… and i have used role in menu for enable and disable.Here  how can i remove multiple if ..else case.Regards,Suraj

  • misko // Dec 30, 2008 at 6:22 am


    In order to get rid of ‘if/case’ you need to replace it with polymorphism. Create a class ‘Role’ and than subclass it into ‘Admin’, ‘Manager’, etc… When you call disable on the ‘Role’ class depending on whether the role is Admin or a Manager different code will execute.

  • Scott // Jan 19, 2009 at 7:51 pm

    Hi Misko;

    I’ve been watching the Clean Code Talks on YouTube and learning lots. Thanks for the effort.

    Sorry to comment late for this posting, but I’m still trying to
    understand something about replacing conditionals with polymorphism.

    I understand how you can extract each “leg” of a conditional into its
    own class, then instantiate the behavior that you want from a factory

    What I don’t understand is how do the factories know which classes to
    instantiate? It seems like the ideal place for a conditional.

    In Ruby we have a few meta helpers like this:

    class PeelerFactory   thin_skinned_fruits = ["apple", "pear"]   my_fruits = []    
    thin_skinned_fruits.each do |f|
         my_fruits << Object.const_get(“#{f.capitalize}Peeler”).new  
    endendclass ApplePeeler; endclass PearPeeler; end

    Is this what you have in mind? Or is it OK
    to use conditionals in factory classes?class PeelerFactory  

    def initialize(type)  @type = type  end  def peeler    case @type    when “apple”    when “pear”    end  end
    class ApplePeeler; end
    class PearPeeler; end


  • misko // Jan 19, 2009 at 8:50 pm

    @Scott, in most cases the conditional ends up in the factory, but sometimes the factory is created polymorphically, so the if ends up even higher up.

  • » Archive » Miško Hevery Programming Talks // Jun 29, 2009 at 11:35 am

    [...] Inheritance, Polymorphism, & Testing [...]

  • Sarah Xia // Nov 5, 2009 at 4:02 am

    Hi Misko,

    Thanks for the great talk!

    I watched it on Youtube last night and couldn’t agree with you more. I went to work this morning and looked at a project I implemented. Boy, so many if statements, so I tried to use polymorphism, and then it was so much easier to understand. My code is so much clearer! I was in a really happy mood for the whole day..even now!

    I feel like my skill somewhere moved up to another level. Thank you!

  • misko // Nov 5, 2009 at 8:44 am


    Glad to be of service.

  • Se hace camino al andar… » Blog Archive » Muerte al if // Mar 1, 2010 at 4:46 pm

    [...] blog. Os recomiendo encarecidamente la siguiente entrada del blog de Miško Hevery titulada “Inheritance, Polymorphism, & Testing” (sí, lo siento, en inglés) porque no tiene desperdicio. Tranquilos, si no os queréis [...]

  • ayumilove // May 2, 2010 at 12:06 am

    In the video, the presenter mention something about not overusing polymorphism that causes runaway subclassing (as in too many classes)

    Where can I find that google clean code talk video focussing on that particular subject?

  • misko // May 3, 2010 at 9:31 am

    @ayumilove, I don’t think we ever recorded that one. :-(

  • ayumilove // May 3, 2010 at 3:44 pm

    my factory has a switch case (if-else statement), how would i use polymorphism to solve this?

  • ayumilove // May 3, 2010 at 3:46 pm

    for instance:
    function createProduct (type : String) : Product
    case PRODUCT_A : return new ProductA();
    case PRODUCT_B : return new ProductB();
    return new NullProduct();

  • karli // Nov 7, 2010 at 5:44 am

    what if u want to implement different interfaces depending on the if statement – i mean in one situation u want web service and in another a smtp on a third a tcp listner – something like log4net does – in this case to implement a web service u have to inherit webservice class — then how do i inherit my own class ?

  • Ramesh.N // May 16, 2011 at 10:09 pm

    Hi Misko,
    Can you please share the ppt used in the video. It would be of great help.

  • misko // May 17, 2011 at 8:39 am


    all talks are here:

  • Scotty // May 17, 2011 at 11:54 am

    Hi Misko,

    When I try to access those talks, it tells me ” e’re sorry, but does not have access to this document”

  • misko // May 17, 2011 at 12:53 pm

    What is the exact link you are having trouble with?

  • Scotty // May 17, 2011 at 5:44 pm

    For example, the “Testing Design Skills – 6 Value vs Service Objects” link:

    But’s it not just that one. I checked 3 or 4 of the peers of that link and got the same result.

  • Ramesh.N // May 22, 2011 at 10:53 am

    Hi Misko,
    Thanks for sharing the link. But i am unable to find
    “The Clean Code Talks – Inheritance, Polymorphism” in the Folder.

    I am also unable to open few links:

    Error: We’re sorry, but does not have access to this document.

Leave a Comment