DEV Community

Discussion on: Simplifying a JavaScript Function With 12 Automated Refactorings

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

As always, I would much prefer the form

const lineChecker = (line, isFirstLine) => {
   if (line === "")
      return `<br />`
   else if (isFirstLine)
      return `<h1>${line}</h1>`
   else
      return `<p>${line}</p>`
}
Enter fullscreen mode Exit fullscreen mode

Or alternatively

const lineChecker = (line, isFirstLine) =>
   (line === "")
      ? `<br>`
      : isFirstLine
         ? `<h1>${line}</h1>`
         : `<p>${line}</p>`
Enter fullscreen mode Exit fullscreen mode

Early return for anything other than errors just makes the code harder to follow and should be avoided whenever it's not really necessary, which is often a sign that a function does too much.