Saturday, February 18, 2017

Substituting polymorphism for if/else or switch/case - Introduction

A few weeks ago I saw this post from a facebook friend:

He is saying jokingly something like "Programming is like love... don't switch() because it will end up with a break... LOL"

I then asked him if his alternative to switch/case is polymorphism. He said no; and that he does not yet know what polymorphism is.

So after a few days, I thought of creating a blog post where I will teach how to replace if/else or switch/case statements with polymorphism.

I'm going to try to teach you how to replace your if/else with polymorphism

Have you seen code like this one below?
      If (typeOfShape is Circle)
      Elseif (typeOfShape is Square)
      Elseif (typeOfShape is Triangle)
Or this one?
      Switch (typeOfShape)
         Case Circle:
         Case Square:
         Case Triangle:
If you have been programming for a few years already, I believe you have seen lots of code that looks like those.

But did you know that, if you are using an object-oriented language such as Java, C# or Python, codes like those ones above are sometimes considered evil?
Evil? (you might say).
Yes! EVIL!

But I did not say that they are always evil. I said that they are sometimes evil. ("Sometimes evil" because I only have a few years of programming experience. "Often evil" when I have more :D . I'm just joking.)

They are evil because they might make modifications in the future much harder than they should be.
(you might be asking…) How else am I going to do conditional constructs like that? If/else and switch/case are the only ones available!
There is actually another method available to us on how to do conditions in OO languages. It is called polymorphism.

I found this great explanation by Steve Guidetti about polymorphism from
Polymorphism is a long word for a very simple concept. 

Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface. 

The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they're all used the same way. 

A real world analogy for polymorphism is a button. Everyone knows how to use a button: you simply apply pressure to it. What a button "does," however, depends on what it is connected to and the context in which it is used -- but the result does not affect how it is used. If your boss tells you to press a button, you already have all the information needed to perform the task.
There are already lots of available resources on the web about polymorphism. [The] Google [search engine] is your friend. Ask your friend to teach you polymorphism. But I recommend you to start with that explanation from I linked to above, or from stackoverflow.

Then after that, go back here because I'm going to give you a number of examples on how to convert your if/else and switch/case statements into polymorphic code.

(Another reason why I delegate the explanation to others is because I'm not very good at explaining things using the English language :) )
Why are you going to give me lots of examples? I think one example will do. 'You think I'm dumb?
No. I do not think you are dumb.

It's only that I read an article a few months ago which claims that giving students lots of examples of a problem and their solutions can help them easily spot similar occurrences of that problem and can help them easily solve those kinds of problems. (But I already forgot where that article is located. Maybe I took a note of it somewhere but I'm not able to find it. I will just update this post when I will find it in the future)

Here are the examples:

1. Drawing Shapes
2. to be posted
3. to be posted
4. to be posted

Examples from other people:

Have fun coding!!!


No comments:

Post a Comment