0

I've a big switch statement in my fragment class. Each case call other functions that for example download an Image and display It, or inflate a layout and show It. Is It a bad pratice? I've read that switch statement can be refactored in order to have a clean code, but how can I clean this code if I call different functions for each case?

Code example

switch(message.type){
    case type.IMAGE:
        downloadAndDisplayImage(); //Download Image and display It in a imageview
        break;
    case type.MAP:
        inflateAndDisplayMap(); //inflate and display a MapView inside a layout
        break;

And so on..

Ferra
  • 91
  • 9

1 Answers1

-1

Switch case smells are well known. If you want to have a "clean" code, a state pattern or the following link can help.

Ways to eliminate switch in code

After all, to whom is it a clean code?

aksappy
  • 3,400
  • 3
  • 23
  • 49
  • Not sure that I can use polymorphism in this case, I just call functions that mainly download something and display in some view It, as in the code example – Ferra Aug 24 '18 at 17:42
  • 1
    Switch statements are no more code smell than any other feature. Sometimes they're the best answer, sometimes not. Adding in a dozen helper cleasses for differnt cases when there's no other good reason to have one is a smell. We don't have enough info here to tell what the right answer is. – Gabe Sechan Aug 24 '18 at 17:45
  • @GabeSechan I have to agree. If there is only one programmer, then I think everything that he is doing is THE standard. – aksappy Aug 24 '18 at 17:47