Eliminating switch and conditional if

I have this piece of code..can this be refactoring


switch(something) {
   case 1:
      foo();
   case 2:
      bar();
      baz();
      break;

   case 3:
      bang();
   default:
      bizzap();
      break;
}
Equivalent with if, with a very awkward default case. And keep in mind that the more fall-through there is, the longer the condition list will get:

if (1 == something) {
   foo();
}
if (1 == something || 2 == something) {
   bar();
   baz();
}
if (3 == something) {
   bang();
}
if (1 != something && 2 != something) {
   bizzap();
}

Let me know if there any better way to write above piece of code
roy_sanuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
I feel the switch is the better way.
Too many Ifs are not good...
0
roy_sanuAuthor Commented:
even i donot like switch ?any another other way to get rid of switch ?  any other design pattern
0
Pawan KumarDatabase ExpertCommented:
These are the only 2 ways as far i know.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Jeffrey Dake Senior Director of TechnologyCommented:
I would really be thinking about how much this will expand in the future. Another way would be to have interfaces for what the 4 cases represent and only if the object is an instanceof that interface call the function.  Another thing that we have used in our code is to have enums that implement a functional interface that does what each section needs to do. I have never been a fan of switch statements because it leads to cases being missed when some new object is added, but just a personal preference.

Hope that helps at all
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David Johnson, CD, MVPOwnerCommented:
Using switch instead of nested if/thens is IMHO a cleaner solution.
0
CEHJCommented:
THIS is much better thinking. I'd probably do something along the lines of using the parameter to switch as a parameter to invoking a factory which built classes that implemented the correct functionality. Of course, a certain amount of if/switch logic would be simply shifted elsewhere
0
roy_sanuAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.