Identifier references in object literals
I suggest to add a rule that requires use of identifiers in object literals for ES6 and TS code:
// Bad:
{foo: foo}
{
foo: computeFoo(),
bar: bar,
baz: 1
}
// Good:
{foo}
{
foo: computeFoo(),
bar,
baz: 1
}
I would not agree: using mixed syntax "breaks" your "scanning/reading patterns" and the shortened property look like something requiring your attention. Plus it has to be managed manually when you feractor a property/variable BTW: literals with all identifier-properties looks really nice
- This rule can be applied to JavaScript style guide too (
tsextendsjsstyle guide). - Personally I use shorthand properties, because I read it faster:
2 steps:
{bar: bar}
^-------------- 1) object has property `bar`
^--------- 2) which has value `bar`. There is additional attention, because value can be different.
vs 1 step:
{bar}
^-------------- 1) object has value `bar` with the same name.
I vote for this rule, we use it in our styleguide and I found out it helps maintain consistent terminology throughout the application.
{
id,
geoObject,
date,
label: caption
}
bends you towards choosing the right word between label and caption much stronger than
{
id: id,
geoObject: geoObject,
date: date,
label: caption
}
In fact, one perhaps will not even notice something is inconsistent in second case. Of course renaming is not possible everywhere, but at least every colon will mean "property and variable names here differ intentionally".
@ikokostya The question is do we use them everywhere possible or only when all properties of an literal are expressible that way?
@dmikis Whenever possible.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.