The long way through Software Craftsmanship

Refactoring functional code

Sep 28, 2016 - 1 minute read - Comments - clean-codefunctional-coderefactoringrefactorcode-snippetsnippet

With my pairing mate we have refactored this piece of functional code.

Original code:

function filterTokens (rawTokens) {
  const cleanedTokens = rawTokens.map(token => (token.length && token[0] === '-') ? token.substring(1) : token)
  return cleanedTokens.filter(token => token !== '' && stopWordsArray.indexOf(token) === -1)
}

Refactored code:

function filterTokens (rawTokens) {
  const cleanedTokens = rawTokens.map(token => startsWithADash(token) ? removeDashFrom(token) : token)
  const processedTokens = cleanedTokens.filter(token => isNotEmpty(token) && isNotAStopWord(token))
  return processedTokens

  function startsWithADash (token) {
    return (token.length && token[ 0 ] === '-')
  }

  function removeDashFrom (token) {
    return token.substring(1)
  }

  function isNotAStopWord (token) {
    return stopWordsArray.indexOf(token) === -1
  }

  function isNotEmpty (token) {
    return token !== ''
  }
 }

These two codes do the same, but the second reads better, given that each small concept (e.g., cleanedTokens) or operation (e.g., removeDashFrom) has a name. Even the returned value, usually result, has an appropiate name. The machine will optimize that variable assignment to a variable but the human reader will dig deeper only if required.

Writing functional code should not be an excuse to forget about clean code. The two are independent variables.

We have found that the second reads better than the first one, but we have reached this conclusion because we have similar values and practices.

Disclaimer about AI/GenAI

As of 2026-05-06, the text in these articles and blog entries has been written without AI/GenAI, except I sometimes use a spellchecker to fix errors. Think Word's spellchecker, not ChatGPT.

Notes, as of today (2026-05-06):

  • No code snippet has been automatically generated, nor vibe-coded, nor generated and reviewed.
  • I don’t have any article with AI contribution.

For future entries:

  • I may have used GenAI for the code in the repo. The code I exemplify/copy in the article will always be reviewed and tested, not vibe-coded. I will specify it in each snippet or at the top/bottom of the article.
  • I normally don't use it for the text contents, although if I have used it for the article text, it would be indicated as such.

Any entry before 2026-05-06 does not contain any AI/GenAI.

For more information, read the AI/GenAI Policy

The Joys of the Craft as Article Support for out of hours on-call support