Hacker News new | ask | show | jobs
by gcmeplz 1122 days ago
I think sometimes "clever" is just a term for bad code that you don't like. The clever-code react example doesn't seem like a good example of clever code to me—it just seems awful! If you were do something clever for that function, it could look like this:

  function extractDataFromResponse ([Component, props]) {
    return _.pickBy({Component, props}, Boolean);
  }
I'm cheating by using lodash, but I think this code is a fairer comparison to the unclever example that's given in the article.
1 comments

Nice simplification of the “clever” code. Lodash docs for anyone curious: https://lodash.com/docs/4.17.15#pickBy

Here’s my version of the “clever” example that doesn’t depend on Lodash. It’s longer than your version but still simpler than the article’s:

  const extractDataFromResponse = (response) => {
    const [Component, props] = response;
    const dataIncludingUndefinedValues = { Component, props };
  
    return Object.fromEntries(
      Object.entries(dataIncludingUndefinedValues).filter(([_key, val]) => val)
    );
  };