12 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.
15 var x = 5; // too short; difficult to understand its purpose without context
20 This rule enforces a minimum and/or maximum identifier length convention.
22 This rule counts [graphemes](https://unicode.org/reports/tr29/#Default_Grapheme_Cluster_Table) instead of using [`String length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length).
26 Examples of **incorrect** code for this rule with the default options:
31 /*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
35 obj.e = document.body;
36 var foo = function (e) { };
49 function foo(...x) { }
52 var { prop: [x]} = {};
56 ({ prop: obj.x } = {});
61 Examples of **correct** code for this rule with the default options:
66 /*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
70 function _f() { return 42; }
71 function _func() { return 42; }
72 obj.el = document.body;
73 var foo = function (evt) { /* do stuff */ };
79 var myObj = { apple: 1 };
80 (num) => { num * num };
81 function foo(num = 0) { }
83 class Foo { method() {} }
84 class Foo { #method() {} }
85 class Foo { field = 1 }
86 class Foo { #field = 1 }
87 function foo(...args) { }
88 function foo([longName]) { }
90 var { prop: [longName] } = {};
92 function foo({ prop }) { }
93 function foo({ a: prop }) { }
96 ({ prop: obj.longName } = {});
97 var data = { "x": 1 }; // excused because of quotes
98 data["y"] = 3; // excused because of calculated property access
103 This rule has an object option:
105 * `"min"` (default: 2) enforces a minimum identifier length
106 * `"max"` (default: Infinity) enforces a maximum identifier length
107 * `"properties": always` (default) enforces identifier length convention for property names
108 * `"properties": never` ignores identifier length convention for property names
109 * `"exceptions"` allows an array of specified identifier names
110 * `"exceptionPatterns"` array of strings representing regular expression patterns, allows identifiers that match any of the patterns.
114 Examples of **incorrect** code for this rule with the `{ "min": 4 }` option:
119 /*eslint id-length: ["error", { "min": 4 }]*/
123 obj.e = document.body;
124 function foo (e) { };
130 var myObj = { a: 1 };
131 (val) => { val * val };
134 function foo(...x) { }
138 var { prop: [x]} = {};
139 ({ prop: obj.x } = {});
144 Examples of **correct** code for this rule with the `{ "min": 4 }` option:
149 /*eslint id-length: ["error", { "min": 4 }]*/
153 function func() { return 42; }
154 obj.element = document.body;
155 var foobar = function (event) { /* do stuff */ };
161 var myObj = { apple: 1 };
162 (value) => { value * value };
163 function foobar(value = 0) { }
165 class Foobar { method() {} }
166 function foobar(...args) { }
168 var [longName] = foo;
169 var { a: [prop] } = {};
170 var { a: longName } = {};
171 ({ prop: obj.name } = {});
172 var data = { "x": 1 }; // excused because of quotes
173 data["y"] = 3; // excused because of calculated property access
180 Examples of **incorrect** code for this rule with the `{ "max": 10 }` option:
185 /*eslint id-length: ["error", { "max": 10 }]*/
188 var reallyLongVarName = 5;
189 function reallyLongFuncName() { return 42; }
190 obj.reallyLongPropName = document.body;
191 var foo = function (reallyLongArgName) { /* do stuff */ };
194 } catch (reallyLongErrorName) {
197 (reallyLongArgName) => { return !reallyLongArgName; };
198 var [reallyLongFirstElementName] = arr;
203 Examples of **correct** code for this rule with the `{ "max": 10 }` option:
208 /*eslint id-length: ["error", { "max": 10 }]*/
212 function funcName() { return 42; }
213 obj.propName = document.body;
214 var foo = function (arg) { /* do stuff */ };
220 (arg) => { return !arg; };
228 Examples of **correct** code for this rule with the `{ "properties": "never" }` option:
233 /*eslint id-length: ["error", { "properties": "never" }]*/
236 var myObj = { a: 1 };
237 ({ a: obj.x.y.z } = {});
238 ({ prop: obj.i } = {});
245 Examples of additional **correct** code for this rule with the `{ "exceptions": ["x"] }` option:
250 /*eslint id-length: ["error", { "exceptions": ["x"] }]*/
254 function x() { return 42; }
255 obj.x = document.body;
256 var foo = function (x) { /* do stuff */ };
262 (x) => { return x * x; };
265 const { a: x } = foo;
270 ### exceptionPatterns
272 Examples of additional **correct** code for this rule with the `{ "exceptionPatterns": ["E|S", "[x-z]"] }` option:
277 /*eslint id-length: ["error", { "exceptionPatterns": ["E|S", "[x-z]"] }]*/
281 function S() { return 42; }
282 obj.x = document.body;
283 var foo = function (x) { /* do stuff */ };
289 (y) => {return y * y};
292 const { a: z } = foo;