mapsapi-codestyle icon indicating copy to clipboard operation
mapsapi-codestyle copied to clipboard

Identifier references in object literals

Open dmikis opened this issue 9 years ago • 7 comments

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
}

dmikis avatar Sep 04 '17 09:09 dmikis

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

vmit avatar Sep 04 '17 10:09 vmit

  1. This rule can be applied to JavaScript style guide too (ts extends js style guide).
  2. 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.

ikokostya avatar Sep 04 '17 12:09 ikokostya

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".

sergets avatar Sep 04 '17 17:09 sergets

@ikokostya The question is do we use them everywhere possible or only when all properties of an literal are expressible that way?

dmikis avatar Sep 04 '17 17:09 dmikis

@dmikis Whenever possible.

ikokostya avatar Sep 05 '17 15:09 ikokostya

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.

stale[bot] avatar Apr 26 '18 22:04 stale[bot]

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.

stale[bot] avatar Jul 06 '18 22:07 stale[bot]