Code Monkey home page Code Monkey logo

eslint-plugin-max-chars-in-function's Introduction

eslint-plugin-max-chars-in-function

ESLint plugin that checks whether functions are too long

Why

V8, the engine behind Node.js has an arbitrary threshold for function length, set at 600 characters, comments included, above which it doesn't optimize the function.

Example:

// Will be optimized
function a(a, b) {
    return a + b;
}

// Will not be optimized
function a(a, b) {
    // Too many characters in this function, mostly due to the comments
    // Too many characters in this function, mostly due to the comments
    // Too many characters in this function, mostly due to the comments
    // Too many characters in this function, mostly due to the comments
    // Too many characters in this function, mostly due to the comments
    // Too many characters in this function, mostly due to the comments
    // Too many characters in this function, mostly due to the comments
    // Too many characters in this function, mostly due to the comments
    // Too many characters in this function, mostly due to the comments
    return a + b;
}

It is also usually considered good practice to have shorter functions.

This rule is pretty much only useful for "code quality" (if you consider short functions to be good functions), and for back-end code, as front-end code usually gets minified, and thus usually gets under that threshold.

How

Install ESLint, globally or locally

$ npm install eslint

Install plugin via npm

$ npm install eslint-plugin-react

Add plugin and rules to your .eslintrc file. Set the value for max-chars-in-function/max-chars-in-func to 1 if you want it to throw warnings, 2 if you want it to throw errors (and 0 to disable the rule, but then you don't need this plugin really).

{
  "plugins": [
    "max-chars-in-function"
  ],
  "rules": {
    "max-chars-in-function/max-chars-in-func": 1
  }
}

If you want to ignore this rule in some of your files (example: mocha test files where the describe clauses are usually big and not really concerned for this rule), add this to your file

/*eslint-disable max-chars-in-function/max-chars-in-func */

function funcThatIsAllowedToBeTooLong() {
    // ...
}

// optionally re-enable the rule
/*eslint-enable max-chars-in-function/max-chars-in-func */

function funcThatMayGiveAWarningOrAnError() {
    // ...
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.