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
85 * `"exceptionPatterns"` array of strings representing regular expression patterns, allows identifiers that match any of the patterns.
89 Examples of **incorrect** code for this rule with the `{ "min": 4 }` option:
92 /*eslint id-length: ["error", { "min": 4 }]*/
96 obj.e = document.body;
103 var myObj = { a: 1 };
104 (val) => { val * val };
107 function foo(...x) { }
111 var { prop: [x]} = {};
112 ({ prop: obj.x } = {});
115 Examples of **correct** code for this rule with the `{ "min": 4 }` option:
118 /*eslint id-length: ["error", { "min": 4 }]*/
122 function func() { return 42; }
123 obj.element = document.body;
124 var foobar = function (event) { /* do stuff */ };
130 var myObj = { apple: 1 };
131 (value) => { value * value };
132 function foobar(value = 0) { }
134 class Foobar { method() {} }
135 function foobar(...args) { }
137 var [longName] = foo;
138 var { a: [prop] } = {};
139 var { a: longName } = {};
140 ({ prop: obj.name } = {});
141 var data = { "x": 1 }; // excused because of quotes
142 data["y"] = 3; // excused because of calculated property access
147 Examples of **incorrect** code for this rule with the `{ "max": 10 }` option:
150 /*eslint id-length: ["error", { "max": 10 }]*/
153 var reallyLongVarName = 5;
154 function reallyLongFuncName() { return 42; }
155 obj.reallyLongPropName = document.body;
156 var foo = function (reallyLongArgName) { /* do stuff */ };
159 } catch (reallyLongErrorName) {
162 (reallyLongArgName) => { return !reallyLongArgName; };
163 var [reallyLongFirstElementName] = arr;
166 Examples of **correct** code for this rule with the `{ "max": 10 }` option:
169 /*eslint id-length: ["error", { "max": 10 }]*/
173 function funcName() { return 42; }
174 obj.propName = document.body;
175 var foo = function (arg) { /* do stuff */ };
181 (arg) => { return !arg; };
187 Examples of **correct** code for this rule with the `{ "properties": "never" }` option:
190 /*eslint id-length: ["error", { "properties": "never" }]*/
193 var myObj = { a: 1 };
194 ({ a: obj.x.y.z } = {});
195 ({ prop: obj.i } = {});
200 Examples of additional **correct** code for this rule with the `{ "exceptions": ["x"] }` option:
203 /*eslint id-length: ["error", { "exceptions": ["x"] }]*/
207 function x() { return 42; }
208 obj.x = document.body;
209 var foo = function (x) { /* do stuff */ };
215 (x) => { return x * x; };
218 const { a: x } = foo;
221 ### exceptionPatterns
223 Examples of additional **correct** code for this rule with the `{ "exceptionPatterns": ["E|S", "[x-z]"] }` option:
226 /*eslint id-length: ["error", { "exceptionPatterns": ["E|S", "[x-z]"] }]*/
230 function S() { return 42; }
231 obj.x = document.body;
232 var foo = function (x) { /* do stuff */ };
238 (y) => {return y * y};
241 const { a: z } = foo;
246 * [max-len](max-len.md)
247 * [new-cap](new-cap.md)
248 * [func-names](func-names.md)
249 * [camelcase](camelcase.md)