# clean-code-javascript

  • 一个函数只做一件事
function emailClients(clients) {
  clients.forEach(client => {
    const clientRecord = database.lookup(client);
    if (clientRecord.isActive()) {
      email(client);
    }
  });
}
1
2
3
4
5
6
7
8
function emailActiveClients(clients) {
  clients.filter(isActiveClient).forEach(email);
}

function isActiveClient(client) {
  const clientRecord = database.lookup(client);
  return clientRecord.isActive();
}
1
2
3
4
5
6
7
8
  • 封装条件
if ((a === 'yes' && !isEmpty(b)) || (c === 'ok')) {

}
1
2
3
function shouldCon(a, b, c) {
  return (a === 'yes' && !isEmpty(b)) || (c === 'ok')
}

if (shouldCon(a, b, c)) {}
1
2
3
4
5