Rather than investing more in switch statements, https://github.com/tc39/proposal-pattern-matching may be more compelling :-)
On Sat, Jan 19, 2019 at 11:23 AM Sm In <[email protected]> wrote: > *Before read: I'm not a native. so I may used some unnatural expression, > or non-sense sentence. I feel so sorry for this. :(* > > ## preface > > currently, a switch statements's case claues compares equality between > it's value and the switch statements's input value. > just like this: > ```js > switch(x) { > case 1: > console.log('x is 1'); > break; > case 2: > console.log('x is 2'); > break; > default: > console.log('x is not 1 and 2'); > } > ``` > > As I know, many books teaches people to use switch statement when there is > many cases for one value. > like above example. > > But, sadly, In many case include above one, the if statement is better > than switch statement. > the above example can be replaced by if statement like blow. > > ```js > if (x === 1) { > console.log('x is 1'); > } else if (x === 2) { > console.log('x is 2'); > } else { > console.log('x is not 1 and 2'); > } > ``` > > code which uses if statement is shorter then switch statement, but we know > **shorter code is not good at anytime**, I mean, sometimes, loger but more > meaningful code is better then shorter one. > So, about that simple example, some people would say "code which uses if > statement is better" and other would say "no, code which uses switch > statement is better". > yes, this far, people could have different opionion about better example. > > but, let's check more complex example. what about checking value's > properties? > maybe it is number, "checking value's properties" can be cheacking is it > integer or real number. > > I think code would be better then my english sentence. here is an example > with if statement. > > ```js > if (x % 1) { > console.log('x is real number'); > } else { > console.log('x is integer'); > } > ``` > > then, here is above example by switch statement. > since switch statement compares equality between it's value and the case > clause's input value. > > ```js > switch(true) { > case x % 1 !== 0: > console.log('x is real number'); > break; > default: > console.log('x is integer'); > } > ``` > > yes, this code is dirty. also it containes redundant codes(which required > by syntax, but in view of human(who reads code), something like `true` or > `x` is useless one). > So, I propose a new syntax for switch statement in order to reduce some > redundant > codes from switch statement. > > ## new syntax for switch statement > > here is an example of the new syntax that works same as above example > > ```js > switch(x) { > case % 1 !== 0: > console.log('x is real number'); > break; > default: > console.log('x is integer'); > } > ``` > > yes, this is it. more meaning then before. > > anyway, let me explain detailed sementic. > > when case clause's expression's most left operend is omitted, switch's > input value(in above example, x is it) gose to that place(omitted operend > place!) > > I will finish my email by showing one more example to you. thank you for > reading this and have a nice day! > > ```js > switch(x) { > case > 10: > console.log('x is bigger then 10'); > break; > case > 5: > console.log('x is bigger then 5, but smaller then 10'); > break; > case > 0: > console.log('x is bigger then 0, but smaller then 5'); > break; > default: > console.log('x is smaller then 0'); > } > ``` > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

