mediamonks / frontend-coding-standards

Media.Monks - Frontend Coding Standards
60 stars 23 forks source link

Add section about default in a switch #37

Closed ThijsTyZ closed 3 years ago

ThijsTyZ commented 3 years ago

I wonder if it's also a good idea to show an object literal example so that people feel that there's an alternative to switches.

Something like this

const selectedGuitar = 'danelectro';

function findGuitar(type) {
  const guitars = {
    fender: 'This is a Fender guitar',
    gibson: 'This is a Gibson guitar',
    danelectro: 'This is a Danelectro guitar',
    default: 'Sorry, We don't have this guitar '
  }
  return guitars[type] || guitars.default
};

findGuitar(selectedGuitar);  --> 'This is a Danelectro guitar'
findGuitar(); --> 'Sorry, We don't have this guitar '

I do not agree with this. Using an object is an other way to handle conditions, but it's not an alternative for a switch. There are too many differences to say that a switch can be replaced with this.