Coding Best Practices (JavaScript) - Write small functions

Janaka Chathuranga
4 min readOct 31, 2018

The idea is simple. Do not write long functions.

Do not write functions longer than 50 lines. — ESLint (Default Configuration)

The default configuration of ESLint enforces that maximum lines in function to 50 lines.

I prefer it to be maximum 20 lines.

The smaller the better. ;)

Why 20 Lines?

It reduces complexity of your code. Makes it easy to understand. Okay, that you knew already I guess. ;) Of course it reduces complexity but there are some small points I want to point out in this article. For that we need to answer the question, why functions were invented. ;)

Why Functions?

Functions or Sub-routines were invented to achieve mainly two things.

  • Re-usability
  • Abstraction

The main thing comes to our mind is Re-usability. Do not write the same code again and again. Write a function to do the thing and use it everywhere. That’s what we do, don’t we? That’s cool. It’s even environment friendly. Reduces the carbon footprint too.

See the world is full of reusable stuff… So, why not our code, of course.

That’s the main purpose of functions, but we don’t pay much attention to the second. Abstraction. At least I didn't. Until…

--

--