Code styles for lists: To put or not to put, the comma before or after?

Recently css/math-wizard Ana Tudor (@thebabydino) asked a question so trivial yet so askworthy at the same time:

Specifically, compiler wise there is no difference between separate var declarations or one list, at least in the V8 javascript engine.
Interestingly, a great coder on stackoverflow -unfortunately my grey matter is shy of remembering and crediting his name-, pointed out that it is good practice to put the comma before. Since then, I somewhat adopted this style, for the following reasoning:


  • Many of us typically read from left to right. When scanning through code, we read even less the right, comma-laden half of the screen. 
  • JSON and the likes be thanks: Developers's typically deal with a lot of commas these days - regardless their language of choice.

When cloning lines (in most of my IDE's, it is setup to CTRL+D  ) with a starting-comma, you won't have to deal with a forsaken comma. This is especially useful when statement-terminators such as a semicolon ; are not common place such as in Python, Coffeescript, Ruby, JSON, YAML, etc.. Otherwise, the duplicated or "cloned-along" semicolon would mess things up just the same.

So

R := x,
y,
z

becomes:

R := x
,y
,z

Afterthought:

Sure, comma-position is more of a personal preference than a company's code-policy. No doubt. As is the personal answer to the question at which point one starts to line-break and indent rather than list everything on one line. But using a pre-comma over a post-comma does make things a wee bit easier, especially during a coder's flow, when line after line fluently appends to the bulk of the code, and magically compiles and continues to compile absent of errors....


an onset of coder-flow. source: photodune  author: photodune; modified 2013
What is your take and what your personal preference?
LihatTutupKomentar