0

I just want to get some information before I go ask my teacher during his offices hours tomorrow.

We have have project to do thats something like an iclicker question answer collector. He told us to avoid using switch case statements. I was just wondering why and why don't people in the field like using them, what alternative is there to do? and I doubt he wants us to use if statements either.

I think we have to use polymorphism/interfaces but I just cant rap my head around that, switch cases seems so much straight forward.

Thank you.

Sam kh
  • 127
  • 1
  • 11
  • 2
    It would be best to just ask the teacher that. – Pokechu22 Oct 09 '14 at 20:15
  • @Pokechu22 lol..hahaha – Kumar Abhinav Oct 09 '14 at 20:15
  • 1
    Did he make that as a general statement, or for this assignment? `switch` is a valid (and valuable) statement in Java and need not be avoided (though some care is required in its use) -- and I would be suspicious of an instructor who flatly told you to never use it. But I can see that, for a given assignment, he might tell you to not use `switch` in order to encourage you to think of other approaches. Very often, a table-based approach is better than a `switch`. (And, in a lot of ways, polymorphism is a glorified form of a table-based approach.) – Hot Licks Oct 09 '14 at 20:15
  • the thing he said is if you find yourself using switch statements, then there might be a better/alternative way to do your project, because you might run in to problems in the future if you need to change something in it. – Sam kh Oct 09 '14 at 20:18
  • 1
    as a rule of thumb, don't use big switch statement blocks if you can implement the logic with another method. when you writing a code, always consider some day, some one wants to read your code and big switch statements are not as Clean as possible. if you you are learning Java or any other programming language, i highly recommend the "Clean Code" book by Robert C.Martin AKA Uncle Bob. – Navid Oct 09 '14 at 20:47

2 Answers2

9

Usually when an instructor asks "don't use feature X", it's because they want you to learn how to do something without using a feature that might be a shortcut. In your case, it sounds like your instructor wants you to wrap your head around polymorphism. If you don't, you won't learn that bit and will have much more trouble later in the class.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

It depends upon the project. For example, in using a RESTful APi, you do have switch statements because there is a limit, known set. But, with your program there might be a lot of different options and that option can change, increase (or decrease), so while you started out with three cases, then something else is wanted, that's four, then five, and so on. You end up with 50 cases, and that's probably not good or easy to maintain.

With your OOP class, the instructor is probably going to show you that. Come back and show the whole problem and the final result, and maybe others can shed light.

There's an example that I've seen in my old Java book, and did a search and see it is still decent. Consider employees and salaries. You have three types of employees, then you have 50 types.

On a small scale, there appears to be not much difference. It requires enlarging the problem and considering consequences.

Ways to eliminate switch in code

That is a good example. Sure, there's only two cases in that example. But, again, what if it were 50? How easy will it be to maintain that? A lot of things in programming are about saving time and making things logical in the long run, as you will be coming back to your code or someone else's, and you have to maintain and support it.

Community
  • 1
  • 1
johnny
  • 19,272
  • 52
  • 157
  • 259