Use object destructuring. Open
message = e.data.error.message;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Prefer destructuring from arrays and objects (prefer-destructuring)
With JavaScript ES6, a new syntax was added for creating variables from an array index or object property, called destructuring. This rule enforces usage of destructuring instead of accessing a property through a member expression.
Rule Details
Options
This rule takes two sets of configuration objects. The first object parameter determines what types of destructuring the rule applies to.
The two properties, array
and object
, can be used to turn on or off the destructuring requirement for each of those types independently. By default, both are true.
Alternatively, you can use separate configurations for different assignment types. It accepts 2 other keys instead of array
and object
.
One key is VariableDeclarator
and the other is AssignmentExpression
, which can be used to control the destructuring requirement for each of those types independently. Each property accepts an object that accepts two properties, array
and object
, which can be used to control the destructuring requirement for each of array
and object
independently for variable declarations and assignment expressions. By default, array
and object
are set to true for both VariableDeclarator
and AssignmentExpression
.
The rule has a second object with a single key, enforceForRenamedProperties
, which determines whether the object
destructuring applies to renamed variables.
Examples of incorrect code for this rule:
// With `array` enabled
var foo = array[0];
// With `object` enabled
var foo = object.foo;
var foo = object['foo'];
Examples of correct code for this rule:
// With `array` enabled
var [ foo ] = array;
var foo = array[someIndex];
// With `object` enabled
var { foo } = object;
var foo = object.bar;
let foo;
({ foo } = object);
Examples of incorrect code when enforceForRenamedProperties
is enabled:
var foo = object.bar;
Examples of correct code when enforceForRenamedProperties
is enabled:
var { bar: foo } = object;
An example configuration, with the defaults array
and object
filled in, looks like this:
{
"rules": {
"prefer-destructuring": ["error", {
"array": true,
"object": true
}, {
"enforceForRenamedProperties": false
}]
}
}
The two properties, array
and object
, which can be used to turn on or off the destructuring requirement for each of those types independently. By default, both are true.
For example, the following configuration enforces only object destructuring, but not array destructuring:
{
"rules": {
"prefer-destructuring": ["error", {"object": true, "array": false}]
}
}
An example configuration, with the defaults VariableDeclarator
and AssignmentExpression
filled in, looks like this:
{
"rules": {
"prefer-destructuring": ["error", {
"VariableDeclarator": {
"array": false,
"object": true
},
"AssignmentExpression": {
"array": true,
"object": true
}
}, {
"enforceForRenamedProperties": false
}]
}
}
The two properties, VariableDeclarator
and AssignmentExpression
, which can be used to turn on or off the destructuring requirement for array
and object
. By default, all values are true.
For example, the following configuration enforces object destructuring in variable declarations and enforces array destructuring in assignment expressions.
{
"rules": {
"prefer-destructuring": ["error", {
"VariableDeclarator": {
"array": false,
"object": true
},
"AssignmentExpression": {
"array": true,
"object": false
}
}, {
"enforceForRenamedProperties": false
}]
}
}
Examples of correct code when object destructuring in VariableDeclarator
is enforced:
/* eslint prefer-destructuring: ["error", {VariableDeclarator: {object: true}}] */
var {bar: foo} = object;
Examples of correct code when array destructuring in AssignmentExpression
is enforced:
/* eslint prefer-destructuring: ["error", {AssignmentExpression: {array: true}}] */
[bar] = array;
When Not To Use It
If you want to be able to access array indices or object properties directly, you can either configure the rule to your tastes or disable the rule entirely.
Additionally, if you intend to access large array indices directly, like:
var foo = array[100];
Then the array
part of this rule is not recommended, as destructuring does not match this use case very well.
Or for non-iterable 'array-like' objects:
var $ = require('jquery');
var foo = $('body')[0];
var [bar] = $('body'); // fails with a TypeError
Further Reading
If you want to learn more about destructuring, check out the links below:
Use object destructuring. Open
message = e.error.message;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Prefer destructuring from arrays and objects (prefer-destructuring)
With JavaScript ES6, a new syntax was added for creating variables from an array index or object property, called destructuring. This rule enforces usage of destructuring instead of accessing a property through a member expression.
Rule Details
Options
This rule takes two sets of configuration objects. The first object parameter determines what types of destructuring the rule applies to.
The two properties, array
and object
, can be used to turn on or off the destructuring requirement for each of those types independently. By default, both are true.
Alternatively, you can use separate configurations for different assignment types. It accepts 2 other keys instead of array
and object
.
One key is VariableDeclarator
and the other is AssignmentExpression
, which can be used to control the destructuring requirement for each of those types independently. Each property accepts an object that accepts two properties, array
and object
, which can be used to control the destructuring requirement for each of array
and object
independently for variable declarations and assignment expressions. By default, array
and object
are set to true for both VariableDeclarator
and AssignmentExpression
.
The rule has a second object with a single key, enforceForRenamedProperties
, which determines whether the object
destructuring applies to renamed variables.
Examples of incorrect code for this rule:
// With `array` enabled
var foo = array[0];
// With `object` enabled
var foo = object.foo;
var foo = object['foo'];
Examples of correct code for this rule:
// With `array` enabled
var [ foo ] = array;
var foo = array[someIndex];
// With `object` enabled
var { foo } = object;
var foo = object.bar;
let foo;
({ foo } = object);
Examples of incorrect code when enforceForRenamedProperties
is enabled:
var foo = object.bar;
Examples of correct code when enforceForRenamedProperties
is enabled:
var { bar: foo } = object;
An example configuration, with the defaults array
and object
filled in, looks like this:
{
"rules": {
"prefer-destructuring": ["error", {
"array": true,
"object": true
}, {
"enforceForRenamedProperties": false
}]
}
}
The two properties, array
and object
, which can be used to turn on or off the destructuring requirement for each of those types independently. By default, both are true.
For example, the following configuration enforces only object destructuring, but not array destructuring:
{
"rules": {
"prefer-destructuring": ["error", {"object": true, "array": false}]
}
}
An example configuration, with the defaults VariableDeclarator
and AssignmentExpression
filled in, looks like this:
{
"rules": {
"prefer-destructuring": ["error", {
"VariableDeclarator": {
"array": false,
"object": true
},
"AssignmentExpression": {
"array": true,
"object": true
}
}, {
"enforceForRenamedProperties": false
}]
}
}
The two properties, VariableDeclarator
and AssignmentExpression
, which can be used to turn on or off the destructuring requirement for array
and object
. By default, all values are true.
For example, the following configuration enforces object destructuring in variable declarations and enforces array destructuring in assignment expressions.
{
"rules": {
"prefer-destructuring": ["error", {
"VariableDeclarator": {
"array": false,
"object": true
},
"AssignmentExpression": {
"array": true,
"object": false
}
}, {
"enforceForRenamedProperties": false
}]
}
}
Examples of correct code when object destructuring in VariableDeclarator
is enforced:
/* eslint prefer-destructuring: ["error", {VariableDeclarator: {object: true}}] */
var {bar: foo} = object;
Examples of correct code when array destructuring in AssignmentExpression
is enforced:
/* eslint prefer-destructuring: ["error", {AssignmentExpression: {array: true}}] */
[bar] = array;
When Not To Use It
If you want to be able to access array indices or object properties directly, you can either configure the rule to your tastes or disable the rule entirely.
Additionally, if you intend to access large array indices directly, like:
var foo = array[100];
Then the array
part of this rule is not recommended, as destructuring does not match this use case very well.
Or for non-iterable 'array-like' objects:
var $ = require('jquery');
var foo = $('body')[0];
var [bar] = $('body'); // fails with a TypeError
Further Reading
If you want to learn more about destructuring, check out the links below:
Use object destructuring. Open
message = e.message;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Prefer destructuring from arrays and objects (prefer-destructuring)
With JavaScript ES6, a new syntax was added for creating variables from an array index or object property, called destructuring. This rule enforces usage of destructuring instead of accessing a property through a member expression.
Rule Details
Options
This rule takes two sets of configuration objects. The first object parameter determines what types of destructuring the rule applies to.
The two properties, array
and object
, can be used to turn on or off the destructuring requirement for each of those types independently. By default, both are true.
Alternatively, you can use separate configurations for different assignment types. It accepts 2 other keys instead of array
and object
.
One key is VariableDeclarator
and the other is AssignmentExpression
, which can be used to control the destructuring requirement for each of those types independently. Each property accepts an object that accepts two properties, array
and object
, which can be used to control the destructuring requirement for each of array
and object
independently for variable declarations and assignment expressions. By default, array
and object
are set to true for both VariableDeclarator
and AssignmentExpression
.
The rule has a second object with a single key, enforceForRenamedProperties
, which determines whether the object
destructuring applies to renamed variables.
Examples of incorrect code for this rule:
// With `array` enabled
var foo = array[0];
// With `object` enabled
var foo = object.foo;
var foo = object['foo'];
Examples of correct code for this rule:
// With `array` enabled
var [ foo ] = array;
var foo = array[someIndex];
// With `object` enabled
var { foo } = object;
var foo = object.bar;
let foo;
({ foo } = object);
Examples of incorrect code when enforceForRenamedProperties
is enabled:
var foo = object.bar;
Examples of correct code when enforceForRenamedProperties
is enabled:
var { bar: foo } = object;
An example configuration, with the defaults array
and object
filled in, looks like this:
{
"rules": {
"prefer-destructuring": ["error", {
"array": true,
"object": true
}, {
"enforceForRenamedProperties": false
}]
}
}
The two properties, array
and object
, which can be used to turn on or off the destructuring requirement for each of those types independently. By default, both are true.
For example, the following configuration enforces only object destructuring, but not array destructuring:
{
"rules": {
"prefer-destructuring": ["error", {"object": true, "array": false}]
}
}
An example configuration, with the defaults VariableDeclarator
and AssignmentExpression
filled in, looks like this:
{
"rules": {
"prefer-destructuring": ["error", {
"VariableDeclarator": {
"array": false,
"object": true
},
"AssignmentExpression": {
"array": true,
"object": true
}
}, {
"enforceForRenamedProperties": false
}]
}
}
The two properties, VariableDeclarator
and AssignmentExpression
, which can be used to turn on or off the destructuring requirement for array
and object
. By default, all values are true.
For example, the following configuration enforces object destructuring in variable declarations and enforces array destructuring in assignment expressions.
{
"rules": {
"prefer-destructuring": ["error", {
"VariableDeclarator": {
"array": false,
"object": true
},
"AssignmentExpression": {
"array": true,
"object": false
}
}, {
"enforceForRenamedProperties": false
}]
}
}
Examples of correct code when object destructuring in VariableDeclarator
is enforced:
/* eslint prefer-destructuring: ["error", {VariableDeclarator: {object: true}}] */
var {bar: foo} = object;
Examples of correct code when array destructuring in AssignmentExpression
is enforced:
/* eslint prefer-destructuring: ["error", {AssignmentExpression: {array: true}}] */
[bar] = array;
When Not To Use It
If you want to be able to access array indices or object properties directly, you can either configure the rule to your tastes or disable the rule entirely.
Additionally, if you intend to access large array indices directly, like:
var foo = array[100];
Then the array
part of this rule is not recommended, as destructuring does not match this use case very well.
Or for non-iterable 'array-like' objects:
var $ = require('jquery');
var foo = $('body')[0];
var [bar] = $('body'); // fails with a TypeError
Further Reading
If you want to learn more about destructuring, check out the links below:
Identical blocks of code found in 2 locations. Consider refactoring. Open
if (e.data && e.data.error && e.data.error.message) {
message = e.data.error.message;
} else if (e.error && e.error.message) {
message = e.error.message;
} else if (e.message) {
- Read upRead up
- Create a ticketCreate a ticket
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 120.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76