constanimalNames={'dog':'πΆ','cat':'π±','parrot':'π¦',};consthandler={get:(_,animalType)=>{return`<div style='font-size:3rem'>${animalNames[animalType]}</div>`||'Unknown';}}constanimals=newProxy({},handler)const{dog,cat,parrot}=animalsdocument.body.innerHTML=`
<div>This is a ${dog}</div>
<div>This is a ${cat}</div>
<div>This is a ${parrot}</div>
`
I'm sorry but at which point a "trick" is an improvement in readability compared to a statement known by any developer in almost all language ?
In my opinion any one liner is usually hurting readability compared to more verbose equivalent because you usually need to stop to understand what's happening in this succession of command
@olivier
I don't think this is a 'trick", this is at least for me, a real feature.
You are absoutely free to use it or not, but everyone should be advertised of those little kown features
I wouldn't think of using the switch statement for this use case.
The example is misleading in that the function getAnimalName doesn't return an animal name but the corresponding symbol for the animal. It should, therefore, be correctly named getAnimalSymbol.
The switch statement in Javascript is pretty verbose, so this is a nice inspiration to think about better solutions.
Switch is usually used with "break", so the code tends to be pretty verbose. But it is able to handle different code in each statement, so an object literal selection is no substitute for switch !!!
Here is a typical - but not very useful - example, just to showcase some options
functionmakeSound(animalType)=>{leta=0switch(animalType){case'dog':alert("bark")a=1breakcase'cat':alert("meuw")breakcase'parrot':alert("make America great again")breakdefault:a=-1}returna};
So, what can we do here to make things better?
Solution A: Better formatting
functionmakeSound(animalType)=>{leta=0switch(animalType){case'dog':alert("bark");a=1;breakcase'cat':alert("meuw");breakcase'parrot':alert("make America great again");breakdefault:a=-1}returna};
Solution B: Use else If
functionmakeSound(animalType)=>{leta=0,N=animalTypeif(N=='dog'){alert("bark");a=1;}elseif(N=='cat'){alert("meuw");}elseif(N=='parrot'){alert("make America great again");}elsea=-1}returna};
Solution C: Use object literal with arrow functions
functionmakeSound(animalType){leta=0constfn={dog:()=>{alert("bark");a=1},cat:()=>{alert("meuw");},parrot:()=>{alert("make America great again")}}letf=fn[animalType]if(f)f()elsea=-1returna};makeSound("dog")
If you do not need to handle the "default" case, this is much better readable:
functionmakeSound(animalType){leta=0constfn={dog:()=>{alert("bark");a=1},cat:()=>{alert("meuw");},parrot:()=>{alert("make America great again")}}fn[animalType]()returna}makeSound("dog")
switch: faster
object: cleaner but consume more memory
For the pleasure only :-)
Play with it here : animals
I'm sorry but at which point a "trick" is an improvement in readability compared to a statement known by any developer in almost all language ?
In my opinion any one liner is usually hurting readability compared to more verbose equivalent because you usually need to stop to understand what's happening in this succession of command
@olivier
I don't think this is a 'trick", this is at least for me, a real feature.
You are absoutely free to use it or not, but everyone should be advertised of those little kown features
Regards
This is not an
all-developerthing. If you are into JS then you learn these kind of things to increase readability.More verbose does not always been better readability, as most people have started to brag about after taking a reverse-brake these days.
I wouldn't think of using the switch statement for this use case.
The example is misleading in that the function getAnimalName doesn't return an animal name but the corresponding symbol for the animal. It should, therefore, be correctly named getAnimalSymbol.
There is no need to write a function for this.
Seems more like a variable name change, not an entire use-case change from this example.
The switch statement in Javascript is pretty verbose, so this is a nice inspiration to think about better solutions.
Switch is usually used with "break", so the code tends to be pretty verbose. But it is able to handle different code in each statement, so an object literal selection is no substitute for switch !!!
Here is a typical - but not very useful - example, just to showcase some options
So, what can we do here to make things better?
Solution A: Better formatting
Solution B: Use else If
Solution C: Use object literal with arrow functions
If you do not need to handle the "default" case, this is much better readable:
This seems to be the standard in Python, as it does not have a
switchstatement.You use
matchinstead ofswitchin pythonThank you. It's the 1st time I'm hearing about
match.Anytime π
very good solution broπ
Let's add another case, chihuahua, which requires you to use the dog emoji, as in the dog case, how would you tackle this with object?