13 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.
16 var x = 5; // too short; difficult to understand its purpose without context
21 This rule enforces a minimum and/or maximum identifier length convention.
25 Examples of **incorrect** code for this rule with the default options:
30 /*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
34 obj.e = document.body;
35 var foo = function (e) { };
48 function foo(...x) { }
51 var { prop: [x]} = {};
55 ({ prop: obj.x } = {});
60 Examples of **correct** code for this rule with the default options:
65 /*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
69 function _f() { return 42; }
70 function _func() { return 42; }
71 obj.el = document.body;
72 var foo = function (evt) { /* do stuff */ };
78 var myObj = { apple: 1 };
79 (num) => { num * num };
80 function foo(num = 0) { }
82 class Foo { method() {} }
83 class Foo { #method() {} }
84 class Foo { field = 1 }
85 class Foo { #field = 1 }
86 function foo(...args) { }
87 function foo([longName]) { }
89 var { prop: [longName] } = {};
91 function foo({ prop }) { }
92 function foo({ a: prop }) { }
95 ({ prop: obj.longName } = {});
96 var data = { "x": 1 }; // excused because of quotes
97 data["y"] = 3; // excused because of calculated property access
102 This rule has an object option:
104 * `"min"` (default: 2) enforces a minimum identifier length
105 * `"max"` (default: Infinity) enforces a maximum identifier length
106 * `"properties": always` (default) enforces identifier length convention for property names
107 * `"properties": never` ignores identifier length convention for property names
108 * `"exceptions"` allows an array of specified identifier names
109 * `"exceptionPatterns"` array of strings representing regular expression patterns, allows identifiers that match any of the patterns.
113 Examples of **incorrect** code for this rule with the `{ "min": 4 }` option:
118 /*eslint id-length: ["error", { "min": 4 }]*/
122 obj.e = document.body;
123 function foo (e) { };
129 var myObj = { a: 1 };
130 (val) => { val * val };
133 function foo(...x) { }
137 var { prop: [x]} = {};
138 ({ prop: obj.x } = {});
143 Examples of **correct** code for this rule with the `{ "min": 4 }` option:
148 /*eslint id-length: ["error", { "min": 4 }]*/
152 function func() { return 42; }
153 obj.element = document.body;
154 var foobar = function (event) { /* do stuff */ };
160 var myObj = { apple: 1 };
161 (value) => { value * value };
162 function foobar(value = 0) { }
164 class Foobar { method() {} }
165 function foobar(...args) { }
167 var [longName] = foo;
168 var { a: [prop] } = {};
169 var { a: longName } = {};
170 ({ prop: obj.name } = {});
171 var data = { "x": 1 }; // excused because of quotes
172 data["y"] = 3; // excused because of calculated property access
179 Examples of **incorrect** code for this rule with the `{ "max": 10 }` option:
184 /*eslint id-length: ["error", { "max": 10 }]*/
187 var reallyLongVarName = 5;
188 function reallyLongFuncName() { return 42; }
189 obj.reallyLongPropName = document.body;
190 var foo = function (reallyLongArgName) { /* do stuff */ };
193 } catch (reallyLongErrorName) {
196 (reallyLongArgName) => { return !reallyLongArgName; };
197 var [reallyLongFirstElementName] = arr;
202 Examples of **correct** code for this rule with the `{ "max": 10 }` option:
207 /*eslint id-length: ["error", { "max": 10 }]*/
211 function funcName() { return 42; }
212 obj.propName = document.body;
213 var foo = function (arg) { /* do stuff */ };
219 (arg) => { return !arg; };
227 Examples of **correct** code for this rule with the `{ "properties": "never" }` option:
232 /*eslint id-length: ["error", { "properties": "never" }]*/
235 var myObj = { a: 1 };
236 ({ a: obj.x.y.z } = {});
237 ({ prop: obj.i } = {});
244 Examples of additional **correct** code for this rule with the `{ "exceptions": ["x"] }` option:
249 /*eslint id-length: ["error", { "exceptions": ["x"] }]*/
253 function x() { return 42; }
254 obj.x = document.body;
255 var foo = function (x) { /* do stuff */ };
261 (x) => { return x * x; };
264 const { a: x } = foo;
269 ### exceptionPatterns
271 Examples of additional **correct** code for this rule with the `{ "exceptionPatterns": ["E|S", "[x-z]"] }` option:
276 /*eslint id-length: ["error", { "exceptionPatterns": ["E|S", "[x-z]"] }]*/
280 function S() { return 42; }
281 obj.x = document.body;
282 var foo = function (x) { /* do stuff */ };
288 (y) => {return y * y};
291 const { a: z } = foo;