Clean up your code by removing ‘if-else’ statements

Clean up your code by removing ‘if-else’ statements

Some tips to make your JavaScript code more elegant.

/**
* Button click event
* @param {number} status
* Activity status: 1 in progress, 2 in failure, 3 out of stock, 4 in success, 5 system cancelled
*/
const onButtonClick = (status)=>{
if(status == 1){
sendLog(‘processing’)
jumpTo(‘IndexPage’)
}else if(status == 2){
sendLog(‘fail’)
jumpTo(‘FailPage’)
}else if(status == 3){
sendLog(‘fail’)
jumpTo(‘FailPage’)
}else if(status == 4){
sendLog(‘success’)
jumpTo(‘SuccessPage’)
}else if(status == 5){
sendLog(‘cancel’)
jumpTo(‘CancelPage’)
}else {
sendLog(‘other’)
jumpTo(‘Index’)
}

}

const onButtonClick = (status)=>{
switch (status){
case 1:
sendLog(‘processing’)
jumpTo(‘IndexPage’)
break
case 2:
case 3:
sendLog(‘fail’)
jumpTo(‘FailPage’)
break
case 4:
sendLog(‘success’)
jumpTo(‘SuccessPage’)
break
case 5:
sendLog(‘cancel’)
jumpTo(‘CancelPage’)
break
default:
sendLog(‘other’)
jumpTo(‘Index’)
break
}

}

const actions = {
‘1’: [‘processing’,‘IndexPage’],
‘2’: [‘fail’,‘FailPage’],
‘3’: [‘fail’,‘FailPage’],
‘4’: [‘success’,‘SuccessPage’],
‘5’: [‘cancel’,‘CancelPage’],
‘default’: [‘other’,‘Index’],
}
const onButtonClick = (status)=>{
let action = actions[status] || actions[‘default’],
logName = action[0],
pageName = action[1]
sendLog(logName)
jumpTo(pageName)

}

const actions = new Map([
[1, [‘processing’,‘IndexPage’]],
[2, [‘fail’,‘FailPage’]],
[3, [‘fail’,‘FailPage’]],
[4, [‘success’,‘SuccessPage’]],
[5, [‘cancel’,‘CancelPage’]],
[‘default’, [‘other’,‘Index’]]
])
const onButtonClick = (status)=>{
let action = actions.get(status) || actions.get(‘default’)
sendLog(action[0])
jumpTo(action[1])

}

  • An object’s key can only be a string or Symbols, but a Map’s key can be any value
  • You can easily get the number of key-value pairs in a Map by using the size attribute, whereas the number of key-value pairs in an object can only be confirmed manually
/**
* Button click event
* @param {number} status
* Activity status: 1 in progress, 2 in failure, 3 out of stock, 4 in success, 5 system cancelled
*
* @param {string} identity: guest, master
*/
const onButtonClick = (status,identity)=>{
if(identity == ‘guest’){
if(status == 1){
//do sth
}else if(status == 2){
//do sth
}else if(status == 3){
//do sth
}else if(status == 4){
//do sth
}else if(status == 5){
//do sth
}else {
//do sth
}
}else if(identity == ‘master’) {
if(status == 1){
//do sth
}else if(status == 2){
//do sth
}else if(status == 3){
//do sth
}else if(status == 4){
//do sth
}else if(status == 5){
//do sth
}else {
//do sth
}
}

}

const actions = new Map([
[‘guest_1’, ()=>{/*do sth*/}],
[‘guest_2’, ()=>{/*do sth*/}],
[‘guest_3’, ()=>{/*do sth*/}],
[‘guest_4’, ()=>{/*do sth*/}],
[‘guest_5’, ()=>{/*do sth*/}],
[‘master_1’, ()=>{/*do sth*/}],
[‘master_2’, ()=>{/*do sth*/}],
[‘master_3’, ()=>{/*do sth*/}],
[‘master_4’, ()=>{/*do sth*/}],
[‘master_5’, ()=>{/*do sth*/}],
[‘default’, ()=>{/*do sth*/}],
])
const onButtonClick = (identity,status)=>{
let action = actions.get(`${identity}_${status}`) || actions.get(‘default’)
action.call(this)

}

const actions = {
  'guest_1':()=>{/*do sth*/},
  'guest_2':()=>{/*do sth*/},
  //....
}const onButtonClick = (identity,status)=>{
  let action = actions[`${identity}_${status}`] || actions['default']
  action.call(this)
}
const actions = new Map([
  [{identity:'guest',status:1},()=>{/* functionA */}],
  [{identity:'guest',status:2},()=>{/* functionA */}],
  [{identity:'guest',status:3},()=>{/* functionA */}],
  [{identity:'guest',status:4},()=>{/* functionA */}],
  [{identity:'guest',status:5},()=>{/* functionB */}],
  //...
])

Conclusion

This article has taught you eight ways to write logical judgments, including:

  1. switch
  2. Unary judgment: stored in Object
  3. Unary judgment: save to Map
  4. Multiple judgment: concatenate the condition into a string and save it in Object
  5. Multiple judgment: concatenate the condition into a string and store it in a Map
  6. Multiple judgment: save condition as an object in Map
  7. Multiple judgment: save condition as a regular expression in Map

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.