question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consistency in lists/objects

See original GitHub issue

Thanks for your work, I’m VERY interested in something that deals with formatting in my place =)

One thing that struck me upon trying it out tonight, though, was that it does not handle “siblings” consistently. That is, given a list of, say, { id, name } objects; if one of them has a name large enough to reach the line limit, then this object, and this object alone, will be printed over 4 lines, while its siblings will be printed on 1 line. The “exploded” one will have a trailing comma, too, while obviously the one-liners will not.

Original test.js:

const myList = [
  {
    id: 0,
    name: 'Short',
  },
  {
    id: 1,
    name: 'Short Too',
  },
  {
    id: 3,
    name: 'Too Long For Your Line Width',
  },
];

Result ($ prettier --bracket-spacing --trailing-comma --print-width 40 test.js):

const myList = [
  { id: 0, name: "Short" },
  { id: 1, name: "Short Too" },
  {
    id: 3,
    name: "Too Long For Your Line Width",
  },
];

I think that in this case, the original is the better code: it’s consistent, easily readable and parsable, at the only cost of taking a lot of vertical space (scrolling is cheap, though).

EDIT: removed JSX example and reworded the issue as per @vramana’s comment.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

10reactions
Augustin82commented, Feb 3, 2017

@vjeux Thank you very much for putting this in, it’s great!

Shouldn’t the same reasoning apply to arrays as well?

Original:

export const appLocales = [
  'en',
  'cn',
  'es',
];

Changed to:

export const appLocales = ['en', 'cn', 'es'];

Expected it to stay the same.

I strongly believe that “pretty” output means “output that can communicate the most information in the most consistent way with as little effort as possible”. For me, being able to know that there are three locales in a split second without having to parse a list and make out if the commas are in, or out, of the quotes, is invaluable, and removing that is making the code less readable.

Thanks for your time and great work (on this and other stuff ^^).

8reactions
gustavopchcommented, Jul 18, 2017

Why aren’t arrays following the same reasoning applied to objects?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lists - Style Manual
Write items in a list so they follow a consistent pattern. The pattern is made up by the number of words you use...
Read more >
Cleanest way to check for consistency of multiple values in an ...
So, you want to check if all the objects in the list have string1..string4 different than the first object of the list. Is...
Read more >
grammatical consistency - guinlist
When a list is given in a single sentence, whether or not as bullet points, each of its parts usually needs the same...
Read more >
Spanner: The secret to Cloud Storage strong list consistency
By offering strong list consistency across all Google Cloud Storage objects, you avoid having to wrangle with these sorts of problems.
Read more >
How to Ensure Internal Consistency of Mutable and Immutable ...
Consistency in Mutable Objects ; public class ; private IList ; public Receipt() { this ; List<PurchaseItem> ; void AddItem(PurchaseItem ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found