Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The value of alignment (at least for me) is not in making the text of the code more readable. Instead, the value is communicating the structure of the code.

Seeing the same shape repeatedly tells me a lot about what is happening. It also helps highlight changes between two lines that are very similar.



> the value is communicating the structure of the code.

I agree and I think that is the same thing as code-readability. Code-readability means you can easily understand the code.

I personally try to abide by what I call "hedge" formatting convention, the punctuation forms a vertical hedge which then shows the structure of your code clearly:

[ a

, b

, c

]

Moving my eyes down to look for the closing bracket is easier (to me) than moving my eyes from left to right to spot it somewhere on the right side of the page.

Note how above ALL the punctuation is in the same (first) column, Having read that it then becomes easier to read what is on the rest of the columns.

In general it helps when things that are the same, or have the same purpose, on each line are in the same horizontal position. It is is then easier to see what is different about each line, since it is easy to see what is the same.


Thanks, I hate it.

But as long as everything is written under the same guidelines, I will accept it.


Languages should allow for leading commas to enable the more uniform:

  [
  , a
  , b
  , c
  ]
Depending on the rest of the language syntax, it could also be nice to have a markdown-style list syntax:

  [
  - a
  - b
  - c
  ]


In JavaScript a leading comma in an array would create a first element whose value is undefined, I believe.


That's correct, but you can use a trailing comma if you prefer uniform lists:

  [
    a,
    b,
    c,
  ]


I simply prefer :

[ a

, b

, c

]

because, I think this is as 'uniform" as it gets. All syntactic separators are in the first column. Thus they are all uniformly in the same column. Line 1 has the syntactic separator '[' which means start-of-array. Lines 2 and 3 have ',' as the syntactic separator which means element-separator. And line 4 has the final syntactic marker which means "end of Array".

All elements of the array start at the same column, uniformly. And all syntactic separators are in the same column also uniformly.


What a stupid design. If the consortium had just one member having the competence of knowing some competing design and using his brain for five minutes, this disaster could have been avoided.

https://news.ycombinator.com/item?id=9624429


Well, JavaScript is pretty much a lost cause anyway.


Huh, I've always wondered about this style of code formatting and never fully understood why people did it.

Your explanation makes good sense. I guess I just wasn't seeing the forest for the trees. ;)


Yes, this is what I've also gravitated towards.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: