1 Replies - 1288 Views - Last Post: 16 July 2009 - 07:22 PM

#1 Tastybrownies   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 13-May 08

My Teacher and the Beauty of Using OOP

Posted 16 July 2009 - 06:59 PM

Okay, so today in my C# II class when talking about GUI and OOP, my teacher talked about new things he wants us to try to do in each of our labs. Basically he said he wants us to try to get away from implementing if statements and switch cases and try to use them as less as possible. Basically, he wants us to take full advantage of all the OOP techniques like; polymorphism, abstract classes, interfaces, and more.

So should I start trying to do this, meaning writing code and finding a way so I don't have to use if statements or something like this?

I figured this would be a good place to ask a question like this because a lot of people on these forums have good real world experience with writing software.

Let me know what you think....

Is This A Good Question/Topic? 0
  • +

Replies To: My Teacher and the Beauty of Using OOP

#2 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5207
  • View blog
  • Posts: 13,955
  • Joined: 18-April 07

Re: My Teacher and the Beauty of Using OOP

Posted 16 July 2009 - 07:22 PM

First of all, no this is not a good thing. And yes it can be a good thing. I shall explain.

Like everything in life, programming techniques should be used in moderation and in situations which really call for them. Classes and such are much different than if or switch statements. Classes is OOP, if and switch are control constructs. They are not interchangeable.

However, what your teacher is probably trying to get you to do is try some new techniques, solve problems from a different perspective, and not rely on things like if statements to be a crutch.

Lets go through an example...

if (car == "red") { // Do stuff }
else if (car == "blue") { // Do more stuff }
else if (car == "green") { // Do more stuff }
else if (car == "black") { // Do even more stuff }
else if (car == "orange") { // Do ugly car stuff }
else if (car == "silver") { // Do futuristic car stuff }
else { // Your car is probably pink and ugly }

The code above is legal code, but it is ugly and can be an eye sore to programmers since the logic is sound but not very clean. In a situation like this, you are using if statements like a crutch. You are probably doing it because you know how if statements work and too scared to try something that may be a bit cleaner. Here you may want to try a switch statement because the problem lends itself to that type of logic and thinking about things. May even save you some typing.

Now you could have a switch statement that gets really really long. Maybe has 20+ cases and takes up more than a full screen. This again should be a red flag that perhaps there is something cleaner and easier to use than relying on the switch statement as a crutch.

Perhaps if you made the car a class that had a list of "car color handlers" in it, you could pass the class the color and it will loop through X number of handler statements and if one matches the color, execute the body of its code. This handler may be another class called "colorHandler".

While I would never go as far as excluding one technique just to use another, the idea is to identify the best technique for the logic at hand and use it with moderation.

Again, I think your teacher is trying to expand your horizons, getting you to think about alternative ways of solving a problem and also trying to make you realize that even though if and switch statements are powerful, they may not always be the answer to your problem.

You will find that much code out there, which is of value, has several classes that feature many if and switch statements in them. You can't code everything just based on classes. You have to make decisions from time to time and that is what if, loops, and switch statements are for.


This post has been edited by Martyr2: 16 July 2009 - 07:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1