1 # enforce minimum and maximum identifier lengths (id-length)
3 Very short identifier names like `e`, `x`, `_t` or very long ones like `hashGeneratorResultOutputContainerObject` can make code harder to read and potentially less maintainable. To prevent this, one may enforce a minimum and/or maximum identifier length.
6 var x = 5; // too short; difficult to understand its purpose without context
11 This rule enforces a minimum and/or maximum identifier length convention.
15 Examples of **incorrect** code for this rule with the default options:
18 /*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
22 obj.e = document.body;
23 var foo = function (e) { };
33 function foo(...x) { }
36 var { prop: [x]} = {};
40 ({ prop: obj.x } = {});
43 Examples of **correct** code for this rule with the default options:
46 /*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
50 function _f() { return 42; }
51 function _func() { return 42; }
52 obj.el = document.body;
53 var foo = function (evt) { /* do stuff */ };
59 var myObj = { apple: 1 };
60 (num) => { num * num };
61 function foo(num = 0) { }
63 class Foo { method() {} }
64 function foo(...args) { }
65 function foo([longName]) { }
67 var { prop: [longName] } = {};
69 function foo({ prop }) { }
70 function foo({ a: prop }) { }
73 ({ prop: obj.longName } = {});
74 var data = { "x": 1 }; // excused because of quotes
75 data["y"] = 3; // excused because of calculated property access
78 This rule has an object option:
80 * `"min"` (default: 2) enforces a minimum identifier length
81 * `"max"` (default: Infinity) enforces a maximum identifier length
82 * `"properties": always` (default) enforces identifier length convention for property names
83 * `"properties": never` ignores identifier length convention for property names
84 * `"exceptions"` allows an array of specified identifier names
88 Examples of **incorrect** code for this rule with the `{ "min": 4 }` option:
91 /*eslint id-length: ["error", { "min": 4 }]*/
95 obj.e = document.body;
102 var myObj = { a: 1 };
103 (val) => { val * val };
106 function foo(...x) { }
110 var { prop: [x]} = {};
111 ({ prop: obj.x } = {});
114 Examples of **correct** code for this rule with the `{ "min": 4 }` option:
117 /*eslint id-length: ["error", { "min": 4 }]*/
121 function func() { return 42; }
122 obj.element = document.body;
123 var foobar = function (event) { /* do stuff */ };
129 var myObj = { apple: 1 };
130 (value) => { value * value };
131 function foobar(value = 0) { }
133 class Foobar { method() {} }
134 function foobar(...args) { }
136 var [longName] = foo;
137 var { a: [prop] } = {};
138 var { a: longName } = {};
139 ({ prop: obj.name } = {});
140 var data = { "x": 1 }; // excused because of quotes
141 data["y"] = 3; // excused because of calculated property access
146 Examples of **incorrect** code for this rule with the `{ "max": 10 }` option:
149 /*eslint id-length: ["error", { "max": 10 }]*/
152 var reallyLongVarName = 5;
153 function reallyLongFuncName() { return 42; }
154 obj.reallyLongPropName = document.body;
155 var foo = function (reallyLongArgName) { /* do stuff */ };
158 } catch (reallyLongErrorName) {
161 (reallyLongArgName) => { return !reallyLongArgName; };
162 var [reallyLongFirstElementName] = arr;
165 Examples of **correct** code for this rule with the `{ "max": 10 }` option:
168 /*eslint id-length: ["error", { "max": 10 }]*/
172 function funcName() { return 42; }
173 obj.propName = document.body;
174 var foo = function (arg) { /* do stuff */ };
180 (arg) => { return !arg; };
186 Examples of **correct** code for this rule with the `{ "properties": "never" }` option:
189 /*eslint id-length: ["error", { "properties": "never" }]*/
192 var myObj = { a: 1 };
193 ({ a: obj.x.y.z } = {});
194 ({ prop: obj.i } = {});
199 Examples of additional **correct** code for this rule with the `{ "exceptions": ["x"] }` option:
202 /*eslint id-length: ["error", { "exceptions": ["x"] }]*/
206 function x() { return 42; }
207 obj.x = document.body;
208 var foo = function (x) { /* do stuff */ };
214 (x) => { return x * x; };
217 const { a: x } = foo;
222 * [max-len](max-len.md)
223 * [new-cap](new-cap.md)
224 * [func-names](func-names.md)
225 * [camelcase](camelcase.md)