Which is better switch statements or separate functions

There is a web application where the client side may perform some operations on an entity called process.
So i made an Enum {process_start, process_edit, process_delete, process_complete}

And on each event i send a message to a particular user.

so i have a function like
sendNofication(userId, event) {

switch(event) {
    case process_start :
   case process_delete:

What are the pros and cons of this approach and an alternatives ?
I can think of one
instead of having various switch case statements
I can have functions corresponding to each event like :

Which is the better appraoch.
Any pros and cons one can see...

Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Fabrice LambertConsultingCommented:
Choice only matter to the amount of statement you'll write within your switch / case.
- Less than 5 lines, keep it.
- More than 5 lines, make functions.

Plus functions can have a stronger meaning than a bunch of lines.
M. TariqApplication DeveloperCommented:
If you are seeing yourself using it in multiple places ,then using function does make more sense then switch/case.
Anthony PangilinanDeveloperCommented:
If there are other things to process for each event, besides sending a notification, it might be better to create an 'Event' interface that contains whatever methods you need (e.g. sendNotification(), logProcess()). Then each event type (Start, Edit, Delete, Complete) can have their own class that implements the 'Event' interface. You can then just call sendNotification() on the 'Event' object, not caring what type it is. This would also make it easier to add new event types in the future. It's a little more setup in the beginning, but it's easier than managing multiple switch statements (again, only if this is just one example of multiple switch statements you need). Again, this is totally overkill if you just have the one thing to process.

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
Rohit BajajAuthor Commented:
In the above case where you talk about creating an Event interface..
What to do in the case where processing of each event type requires different data. in that case the parameters of sendNotification() will be different.
Anthony PangilinanDeveloperCommented:
What to do in the case where processing of each event type requires different data.

You can try putting the data into an object and figuring it out within the different implementations, or you can have all the possible arguments in the method signature, but nullable, and each method implementation can just look for what it needs. The first option is cleaner, but might be unnecessary if it's only a difference of a couple fields.
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

From novice to tech pro — start learning today.