Hacker News new | past | comments | ask | show | jobs | submit login

So for long lines to move an argument over to align with its predecessor on the previous line, like

    int f() {
        int someVariableName = myFunctionCall(expr(5),          // <--- line A
                                              somethingElse(9), // <--- line B
                                              a_long * expression + involving(multiple.subExprs));
        int d = somethingElse; // <-- line C
    }
...you're saying line A is indented with tabs, and line B with spaces? Because that will misalign if your tab setting isn't the same.

Or are you saying that both A and B are indented with spaces? Then what about line C, which will misalign with A and B if your tab stop is a different width?

Or do we indent the entire body of f() with spaces, and then in that case you're basically using spaces everywhere, which the good idea that sparked this whole thing?




I think he's saying use tabs on lines A, B, and C up until the indentation level (the "int"). Then on line B use spaces for all the whitespace from there until the text is aligned.

    /*t*/int f() {
    /*tabs*/int someVariableName = myFunctionCall(expr(5),          // <--- line A
    /*tabs*//*--------------spaces--------------*/somethingElse(9), // <--- line B
    /*tabs*//*--------------spaces--------------*/a_long * expression + involving(multiple.subExprs));
    /*tabs*/int d = somethingElse; // <-- line C
    /*t*/}


There's no reason to do this silly mix of tabs and spaces. Just move all parameters to a newline have have sane consistency everywhere:

    int f() {
    <tab> someVariableName = myFunctionCall(
    <tab><tab> expr(5),
    <tab><tab> somethingElse(9),
    <tab><tab> a_long * expression + involving(multiple.subExprs)
    <tab> );
    <tab> d = somethingElse;
    }


You indent line A with tabs. You use tabs to line the start of line B up with the start of line A (indentation) and then use spaces to move it the rest of the way to where you want it (alignment).


And as an aside, it's worth pointing out that most editors will handle the initial tab part automatically, so it's not like you have to do mental math to think "okay, this many tabs, and now switch to spaces". Just at the end of line A, you hit enter and start hammering the space bar.


I think this is what parent meant:

  1 int f() {
  2 >---int someVariableName = myFunctionCall(expr(5),
  3 >---                                      somethingElse(9),
  4 >---                                      a_long * expression + involving(multiple.subExprs));
  5 >---int d = somethingElse;
  6 }
Tabs only for indentation (lines 2 to 5), spaces for alignment (lines 3 and 4). This is easy to achieve if you use Vim with Smart Tabs for instance (https://vim.sourceforge.io/scripts/script.php?script_id=231).


A is indented with a tab.

B is indented with a tab, plus the requisite number of spaces to line up with the above line.

C is indented with a tab.


Does anyone actually do that? I've never noticed that style in any code I've read, but maybe that just means it works.


I don't think anyone manually counts out their tabs and spaces, but I think every major text editor has a mode or plugin to make this automatic.




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: