DEV Community

Lars-Erik Bruce
Lars-Erik Bruce

Posted on

1

The Overuse of Arrow Functions: A Cautionary Tale

In the world of modern JavaScript, there's a trendy tool that many developers have latched onto like a lifebuoy in a stormy sea - the arrow function. While arrow functions do have their place, it seems like everyone's using them for everything these days. And honestly, it's starting to get a bit annoying.

Arrow functions are touted for their concise syntax and implicit return, which can make simple functions look cleaner. But here's the thing: not everything in coding is simple, and not every function benefits from this conciseness. Let's talk about why the overuse of arrow functions can actually make your code less readable and more awkward.

Short and Simple Is Good, but Not Always

Sure, arrow functions are great for short, one-liner functions. They save you from typing out the word "function" and the curly braces. But what about those functions that do more than one thing? Arrow functions force you to use parentheses and braces, making your code look cramped and less intuitive.

// Arrow function for a simple one-liner
const add = (a, b) => a + b

// Arrow function for a more complex function
const multiply = (a, b) => {
  const result = a * b
  console.log(result)
  return result
}

// Write it as a proper function instead!
function multiply(a, b) {
  const result = a * b
  console.log(result)
  return result
}
Enter fullscreen mode Exit fullscreen mode

Readability Matters

Coding is not just about getting the job done; it's about making your code readable and maintainable. Overusing arrow functions can make your code look like a jumble of parentheses and arrows, making it harder for your teammates (and even your future self) to understand what's going on. Look at the example above, the regular function takes less space than the arrow function!

The Mysterious this Keyword

Arrow functions are notorious for their lexical this binding. It sounds fancy, but it can be confusing. Regular functions have dynamic this, which makes it easier to understand where this is coming from. Arrow functions might surprise you when you expect this to refer to the object you're working with.

const person = {
  name: "John",
  sayHi: () => {
    console.log(`Hello, I'm ${this.name}`) // Oops, this is not what we expected!
  },
};

Enter fullscreen mode Exit fullscreen mode

Where arrow functions shine!

When writing simple maps and filters, in other hand, arrow functions might be excellent, and make your code much more readable than the wordy function-function:

const angrySheeps = sheeps.filter((sheep) => sheep.isAngry);
Enter fullscreen mode Exit fullscreen mode

Conclusion

So, let's be clear: arrow functions are not the enemy. They're a powerful tool, but they should be used sparingly. Don't be in such a rush to shave off a few characters from your code that you sacrifice readability and maintainability.

I used ChatGPT to help me formulate parts of this post. All opinions are sincerely mine, and based on my experience as a professional software developer.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more