Highlighted

Which do you prefer? Switch/case, if/else, dispatch table?

LEGEND ,
Sep 14, 2018

Copy link to clipboard

Copied

Hello, all,

I asked a question in the Javascript forum at SP, just an opinion piece, and thought I'd go ahead and ask it, here, too.

This pertains to Javascript, specifically.

When I first started developing, the only conditional I used was if/else. A few years later, I learned about switch/case and started using it for every conditional that could be done with switch/case, falling back to if/else for situations where switch/case couldn’t be used.

About four years ago, I learned about dispatch tables and I’m going bonkers with it. I would be using dispatch tables in my professional work, but no one else here wants to use them, so I stick to switch/case and if/else just because no one else wants to know about them and I have to keep my code so that anyone can work on it. (heavy sigh).

What is your preference?

V/r,

^ _ ^

PS:  An example of a Javascript dispatch table can be found, here.

Views

376

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Which do you prefer? Switch/case, if/else, dispatch table?

LEGEND ,
Sep 14, 2018

Copy link to clipboard

Copied

Hello, all,

I asked a question in the Javascript forum at SP, just an opinion piece, and thought I'd go ahead and ask it, here, too.

This pertains to Javascript, specifically.

When I first started developing, the only conditional I used was if/else. A few years later, I learned about switch/case and started using it for every conditional that could be done with switch/case, falling back to if/else for situations where switch/case couldn’t be used.

About four years ago, I learned about dispatch tables and I’m going bonkers with it. I would be using dispatch tables in my professional work, but no one else here wants to use them, so I stick to switch/case and if/else just because no one else wants to know about them and I have to keep my code so that anyone can work on it. (heavy sigh).

What is your preference?

V/r,

^ _ ^

PS:  An example of a Javascript dispatch table can be found, here.

Views

377

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 14, 2018 0
Adobe Community Professional ,
Sep 14, 2018

Copy link to clipboard

Copied

I like chocolate except on Sundays when I prefer cherry .

Dispatch tables are great if you can use them.  But it really depends on the situation and how complex the function is to begin with. 

Nancy O'Shea, ACP
Alt-Web.com

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 14, 2018 0
LEGEND ,
Sep 14, 2018

Copy link to clipboard

Copied

Oh, absolutely.  I mean, if all I'm looking for is a boolean, if/else is the shiznit.  But for other, more complex things, I really like that it's (slightly) faster to access an object property than it is to do a string comparison.

V/r,

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 14, 2018 1
Adobe Community Professional ,
Sep 14, 2018

Copy link to clipboard

Copied

I have never had a reason to use dispatch tables, but I can see their worth in cutting down scripting time compared to the switch operator. As far as processing time is concerned, I doubt that there would be much of a difference like there is between multiple if/else statements.

For boolean values I tend to use the ternary operator.

Just my two bobs worth.


Ben

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 14, 2018 1
LEGEND ,
Sep 15, 2018

Copy link to clipboard

Copied

Personally lve never heard of dispatch table.

I would think you would use the workflow and set the code out which is meaningfull to yourself.

I know one thing, for everyone that says this is what you should be using or doing there will be another with an opposing view and workflow also thinking their methods are the best approach.

Its a problem with web development, because there is no difinitive way, the only way is what the developer understands and knows and ultimately works.

Will l be investigating dispatch tables, no, never needed it so  lm thinking well lve got this far without even knowing about it so its not a necessity which is going to change my game radically enough to be considering unlike some othet development approaches which l do feel need further investigation.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2018 0
Adobe Community Professional ,
Sep 15, 2018

Copy link to clipboard

Copied

relatively long ago we had a process in place that allowed the same approach, we called this technique look up table... then when object-oriented programming appeared, it was very flexible to set up a dedicated class with some static properties that became flexible to use...

now with JSON and the object principle of functions it opens a large number of doors especially in functional programming, where it is possible to pass the value of a property as a function, instead of calling an if then else tree to then call an action in return... most libs out there are build on that way... included jQuery

now as pointed Ben I love using the ternary operator... and now json lookuptable can be mixed with it too....

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2018 1
LEGEND ,
Sep 15, 2018

Copy link to clipboard

Copied

I must be unusual in that respect becuse lve always found some shortened worflows such as the ternary operator, nesting, some css attributes like bg etc less logical to read as l go through the code, indented code annoys the hell out of me too..........then again some like to bulge up their html code with multiple classes and redundant div soup if you are using a front end framework,, cms etc so its six of one and half a dozen of the other.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2018 0
Adobe Community Professional ,
Sep 15, 2018

Copy link to clipboard

Copied

we are just talking about tools... then, as with all tools, everyone is free to use them or not... there is no obligation...

as far as I am concerned, and so I speak in a very subjective way, I realized that by using tools from the world of object-oriented programming, as well as the new tools that have appeared with the democratization of HTML5, and therefore of JavaScript, I produce faster, more flexible, and more scalable...

I can manage several projects simultaneously and it is possible for me to take over quite old projects and to be able to come back to life very quickly...

so these tools are more suitable for me, but again, I'm subjective

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2018 0
LEGEND ,
Sep 15, 2018

Copy link to clipboard

Copied

I use tools, if you want to refer to  code as tools, in the same way as you do, how l see them fitting in with my workflow. Everyone is free to use these tools using optional approaches, which makes the work they produce  faster and  more flexible where they are concerned. 

If you use a dark coding environment because its better for you  use it, alternatively if you like a light coding environment because its better for you then use it. As you say its all subjective and an individual choice.

Anyone can manage several projects and often do so lm not quite sure what your point is here.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2018 0
Adobe Community Professional ,
Sep 15, 2018

Copy link to clipboard

Copied

when I talk about tools, I mean instruments that allow us to obtain a consequence... the code is not the tool... the code is the environment...

so within the code (and this whatever the language used) we have tools...

with regard to the state conditions, we have several tools at our disposal......

  • structures if the else, are one of them...
  • the swicth case is another one
  • the look up tables yet another one
  • the ternary operator also
  • and so on......

none of them is more obscure (or dark) than another, they are just different and therefore allow us different things

their readability depends just on habit and practice

what I mean by leading several projects simultaneously... is that before I surrounded myself with tools of this kind, I produced a certain amount of work every day, and this at a certain cost... today.... in the same period of time, I can produce much more and therefore invest myself on several projects simultaneously ... thus increasing the gain, both in volume and in income

then because of the object aspect of the methods, a large reuse of the logics allows to quickly find the same algorithmic approaches and therefore to return more easily to old projects (such as returning to known terrain)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2018 0
osgood_ LATEST
LEGEND ,
Sep 15, 2018

Copy link to clipboard

Copied

I think different component options  within a specific language is a better fit than tools, thats just a minor language issue l think.

I agree about the readability angle.Whats good for one is not necessarily good for another.. l dont think performance matters that much as the difference would be minimal. I seem  to remember reading some years back that the ternary operator in php didnt perform as well in some situations as the if,else statement. That could of course have improved in recemt years.

Depends on what workflow you use l think in terms of productivity and financial gain. Not all are equal in the respect of producing acceptable coding particularly if you use front end frameworks and content managment systems like WP. Its said that they might improve productivity but at what cost............its more package holiday then exclusive and l dont offer package holiday as a service.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2018 0