by Miško Hevery
Google Tech Talks
November 20, 2008
ABSTRACT
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.
22 responses so far ↓
Thanks a lot for these talks… I couldn’t make it to Google (being located in Romania, Eastern Europe
) 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
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
Regards,
nazgob
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
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
@suraj
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.
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
class.
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” ApplePeeler.new when “pear” PearPeeler.new end end
end
class ApplePeeler; end
class PearPeeler; end
Thanks!Scott
@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.
mrclay.org » Archive » Miško Hevery Programming Talks // Jun 29, 2009 at 11:35 am
[...] Inheritance, Polymorphism, & Testing [...]
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!
@Sarah,
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 [...]
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?
@ayumilove, I don’t think we ever recorded that one.
my factory has a switch case (if-else statement), how would i use polymorphism to solve this?
for instance:
function createProduct (type : String) : Product
{
switch(type)
{
case PRODUCT_A : return new ProductA();
case PRODUCT_B : return new ProductB();
}
return new NullProduct();
}
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 ?
Hi Misko,
Can you please share the ppt used in the video. It would be of great help.
@Ramesh,
all talks are here: https://docs.google.com/leaf?id=0B7z6S2TvsDWSN2Q1ZGI5NzktOTUxYi00Yzk4LWFhYWQtMWE1OGU5NWQzNDVl&hl=en
Hi Misko,
When I try to access those talks, it tells me ” e’re sorry, but xxxxxx@gmail.com does not have access to this document”
What is the exact link you are having trouble with?
For example, the “Testing Design Skills – 6 Value vs Service Objects” link: https://docs.google.com/leaf?id=0AYvU0v65fckFYWR6cWgybTRocV8yNTZjZGpkenZmaw&sort=name&layout=list&pid=0B7z6S2TvsDWSN2Q1ZGI5NzktOTUxYi00Yzk4LWFhYWQtMWE1OGU5NWQzNDVl&cindex=8
But’s it not just that one. I checked 3 or 4 of the peers of that link and got the same result.
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:
https://docs.google.com/present/edit?id=0AYvU0v65fckFYWR6cWgybTRocV8yNTZjZGpkenZmaw&hl=en_US
Error: We’re sorry, but xxxxx@gmail.com does not have access to this document.
Leave a Comment