functionsignIn(user,password){if(user==='bob'&&password===''){returntrue}elseif(user==='sally'&&password==='gitIsCool7#6'){returntrue}elseif(user==='jerry'&&password==='hackme123'){returntrue}}signIn('jerry','hackme123');//will return ability to sign in (boolean)
@naveennamani
is right. There are ways to do things with an array, but it can sometimes get over complicated and unreadable.
I see your point, but what if there is a property that is completely unrelated to the other variables? It would be pointless to do it for each one.
functionsignIn(user,password){if(user==='bob'&&password===''){returntrue}elseif(user==='sally'&&password==='gitIsCool7#6'&&itIsMorning){returntrue}elseif(user==='jerry'&&password==='hackme123'&&SallyIsOffline){returntrue}}signIn('jerry','hackme123');//will return ability to sign in (boolean)
And sorry for jumping in like so in the thread, I just know that plenty of people are for some reason struggling with this approach and I found this example intriguing. Also want to add that there is no practical way to completely avoid if/else statements, I use both approaches in my every day coding, it's just a matter of improving readability and removing code duplication.
If there's a simple 0 and 1 check, I see no reason to use a lookup map for that case, I'd just write down a simple if.
sure those are equivalent, but what above the functionality that i intended to run in the if condition, should I wrap them in anonymous functions and pass to another function. What about the else if condition? Should I call that like
if(example('b', b))
?
If all conditions has some common logic you can use mapping to bring the variability into the object and using the mapped values to execute the logic. But again, even if slight deviation is added (for example a console log in a single else if condition) then your common logic is not common anymore, and you have to add more hacks like anonymous functions as part of the mapping object and so on, as you just demonstrated in your example with validation functions.
The best practical example would be state management in redux, where switch case statements seems to be most efficient, readable and manageable, try implementing that with mapping.
Hey @naveennamani
, appreciate the time you've taken to think about this. My comment was a reply, not a statement, where I simply described how a specific problem could be written in a different kind of way with an example. Like I wrote, I use if/else, switch and this kind of approach on every day basis, based on the situation. If a function is supplied with too much logic, there's probably something that's gonna go wrong and it could be split into separate pure parts.
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This only works if you're comparing a single variable with known values. For other cases you've to use if/else
Can you share any example where you cannot make use of this?
I meant a real example from code. Anyways, we need to write good code in order to not add conditionals like these.
This is a perfectly fine example of real code lol.
Here:
@naveennamani is right. There are ways to do things with an array, but it can sometimes get over complicated and unreadable.
There are still elegant ways to handle that kind of requirement within the function:
And this translated to the initial
a
andb
example becomes:I see your point, but what if there is a property that is completely unrelated to the other variables? It would be pointless to do it for each one.
With lookup maps approach you can drill down how much you wantt with minimal changes to the initial structure, so it would be something like this:
Now you can go even further and have multiple validity checks for example:
And sorry for jumping in like so in the thread, I just know that plenty of people are for some reason struggling with this approach and I found this example intriguing. Also want to add that there is no practical way to completely avoid
if/else
statements, I use both approaches in my every day coding, it's just a matter of improving readability and removing code duplication.If there's a simple
0
and1
check, I see no reason to use a lookup map for that case, I'd just write down a simpleif
.This comment, and example is the best to explain the concept, thanks for this!
@davidlazic are you saying i should replace
with
sure those are equivalent, but what above the functionality that i intended to run in the if condition, should I wrap them in anonymous functions and pass to another function. What about the else if condition? Should I call that like
if(example('b', b))
?
If all conditions has some common logic you can use mapping to bring the variability into the object and using the mapped values to execute the logic. But again, even if slight deviation is added (for example a console log in a single else if condition) then your common logic is not common anymore, and you have to add more hacks like anonymous functions as part of the mapping object and so on, as you just demonstrated in your example with validation functions.
The best practical example would be state management in redux, where switch case statements seems to be most efficient, readable and manageable, try implementing that with mapping.
Hey @naveennamani , appreciate the time you've taken to think about this. My comment was a reply, not a statement, where I simply described how a specific problem could be written in a different kind of way with an example. Like I wrote, I use if/else, switch and this kind of approach on every day basis, based on the situation. If a function is supplied with too much logic, there's probably something that's gonna go wrong and it could be split into separate pure parts.