Hacker News new | ask | show | jobs
by pavel_lishin 4905 days ago
I'm a convert to spaces after someone pointed out that it's easier to make your code 'pretty'.

    // This doesn't look very nice
    thing = { some_key_1: some_var_1,
    [->][->][->]some_key_2: some_var_2,
    [->][->][->]some_key_3: some_var_3 }
    
    // Neither does this.
    thing = { some_key_1: some_var_1,
    [->][->]some_key_2: some_var_2,
    [->][->]some_key_3: some_var_3 }
    
    // Feels good, man.
    thing = { some_key_1: some_var_1,
              some_key_2: some_var_2,
              some_key_3: some_var_3 }
    
    // I want to beat you with an oar.
    thing = { some_key_1: some_var_1,
    [->][->]  some_key_2: some_var_2,
    [->][->]  some_key_3: some_var_3 }
6 comments

Use tabs to align stuff on the left. This means putting some_key_1 on the next line. Also move the closing brace to a new line and add a comma after some_var_3.

Added advantage that you can move all of the lines around, :sort them or comment out individual ones without breaking anything.

Use spaces to align anything after the key, I usually align them with a tab stop so that several blocks line up.

  thing = {
  [->]some_key_1: some_var_1,
  [->]some_key_2: some_var_2,
  [->]some_key_3: some_var_3,
  }
I use an alternate form that doesn't require alignment to an arbitrary column and works with tab indentation without needing to mix:

  thing = {
  [→][→]some_key_1: some_var_1,
  [→][→]some_key_2: some_var_2,
  [→][→]some_key_3: some_var_3,
  [→]}
Several benefits: easier to insert lines at the head or tail without making the SCM diff ugly, also avoids merge conflicts in those cases, and I use indentation of two levels to separate it from code.

Why suffer a problem when it's easily avoided? :)

Watch out or you'll get

    // I want to beat you with an oar.
    thing = {[->]some_key_1: some_var_1,
    [->][->][- >]some_key_2: some_var_2,
    [->][->][- >]some_key_3: some_var_3 }
Another tabs-only approach is to also add a single tab before some_key_1.
When working on code bases that follow this style, there tends to be extra whitespace turn when new code is added to re-align everything back up.
What's wrong with the "I want to beat you with an oar" approach? This is what I do and it works great.