facn2 / Alpha.HAML

Autocomplete project
0 stars 2 forks source link

Destructuring requires for code clarity #71

Open finnhodgkin opened 7 years ago

finnhodgkin commented 7 years ago

This is more personal preference thing (I :heart: destructuring).

This:

const { handleHome, handleAuto, handleIndex, handleCss, handleFavicon } = require('./handlers');

const router = (request, response) => {
  const url = request.url;
  if (url === '/') {
    handleHome(response);
  } else if (url.indexOf('/auto') !== -1) {
    handleAuto(request, response);
  } else if (url.indexOf('/index.js') !== -1) {
    handleIndex(request, response);
  } else if (url.indexOf('/main.css') !== -1) {
    handleCSS(request, response);
  } else if (url.indexOf('/favicon.ico') !== -1) {
    handleFavicon(request, response);
  } else {
    response.writeHead(404, "Content-Type:text/html");
    response.end("<h1>Heather fucked up</h1>");
  }
}

module.exports = router;

Is functionally the same as:

const handlers = require('./handlers');

const router = (request, response) => {
  const url = request.url;

  if (url === '/') {
    handlers.handleHome(response);
  } else if (url.indexOf('/auto') !== -1) {
    handlers.handleAuto(request, response);
  } else if (url.indexOf('/index.js') !== -1) {
    handlers.handleIndex(request, response);
  } else if (url.indexOf('/main.css') !== -1) {
    handlers.handleCSS(request, response);
  } else if (url.indexOf('/favicon.ico') !== -1) {
    handlers.handleFavicon(request, response);
  } else {
    response.writeHead(404, "Content-Type:text/html");
    response.end("<h1>Heather fucked up</h1>");
  }
}

module.exports = router;
finnhodgkin commented 7 years ago

Why am I assigned? :laughing:

tyeung2017 commented 7 years ago

notes taken. this is a very nice approach.

I was thinking about using array to store the route. but your method is even better, less key stroke~