Skip to content

Instantly share code, notes, and snippets.

@makeusabrew
Created January 4, 2013 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makeusabrew/4451255 to your computer and use it in GitHub Desktop.
Save makeusabrew/4451255 to your computer and use it in GitHub Desktop.
Various object key / val alignment styles
// 1) colon aligned, no spaces where possible
var Foo = {
host :"foo",
db :"bar",
details:"baz"
};
// 2) colon aligned, space after key
var Foo = {
host : "foo",
db : "bar",
details: "baz"
};
// 3) colon aligned, space before & after key
var Foo = {
host : "foo",
db : "bar",
details : "baz"
};
// 4) space aligned, no spaces where possible
var Foo = {
host: "foo",
db: "bar",
details:"baz"
};
// 5) space aligned, space after key
var Foo = {
host: "foo",
db: "bar",
details: "baz"
};
// 6) space aligned, space before & after key
var Foo = {
host : "foo",
db : "bar",
details : "baz"
};
// or something else entirely?
@makeusabrew
Copy link
Author

Personally I try and stick to 2... but often meander into 3... or 1. Pretty hopeless really.

@mheap
Copy link

mheap commented Jan 4, 2013

Generally 2 or no alignment at all. One thing I do do though, is make sure keys are quoted. For some reason unquoted keys just don't sit right with me

@stephenmelrose
Copy link

#5, but without the alignment.

var Foo = {
  host: "foo",
  db: "bar",
  details: "baz"
};

@JamieMason
Copy link

The same as @stephenmelrose for me, I used alignment for a while but didn't like all the whitespace diffs in commits - just preference.

@makeusabrew
Copy link
Author

@stephenmelrose & @JamieMason - interesting, I go with #5 sometimes (mainly as I have zero consistency) but always feel guilty about it. Whitespace diff is a good point.

@mheap - too much JSON in your life :D

@mheap
Copy link

mheap commented Jan 4, 2013

@JamieMason @makeusabrew - You can ignore whitespace in diffs. Just use the -w flag on the command line, or add ?w=0 to any Github diff URL's to ignore it

@mtarbit
Copy link

mtarbit commented Jan 4, 2013

Another vote for #5, sometimes with alignment, sometimes without.

@stephenmelrose
Copy link

I just don't like the alignment, I find it much easier to read without, which I know is strange. I also think it's neater without.

@stevefrost
Copy link

#5 for me, but I occasionally slip in to the same as stephenmelrose

@JamieMason
Copy link

Ah cool, thanks for the tip @mheap.

@stephenmelrose
Copy link

#5 without alignment fits nicely and consistently with 1 line definitions too, e.g.

var Foo = { host: "foo", db: "bar" };
var Foo = {
  host: "foo",
  db: "bar",
  details: "baz"
};

@makeusabrew
Copy link
Author

Believe it or not I was going to list various other options without indentation (such as @stephenmelrose's variation of #5) but thought I'd get burnt at the stake for even suggesting such a thing. Good to know :)

@rowanmanning
Copy link

#5 but without alignment, same as @stephenmelrose

@davedevelopment
Copy link

I'm with @stephenmelrose

@makeusabrew I think comma-first would have got you burnt at the stake :)

@glenjamin
Copy link

Same as steve for this, the real question is where to put the newlines and how much to indent when passing an object literal as a function argument!

@stephenmelrose
Copy link

@CyberStrike
Copy link

CyberStrike commented Jul 12, 2017

Wow, this is 4-year-old thread. But this how I align things.

Right align keys on colon. Values left aligned with space after colon.

{
           key: 1,
        string: "value",
    object_key: {
                   string_key: "string",
                  integer_key: 2
                },
  function_key: () => {

                }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment