| ▲ | gte525u a day ago |
| This works until someone tries to vertically align something like a table or a line that is wrapped. |
|
| ▲ | Arelius a day ago | parent | next [-] |
| Yes, this. Which the counter point is either, Don't align things, or use tabs for indentation, and spaces for alignment. And maybe you can enforce no alignment, but that's a hard fight to win. And as far as tabs for indentation and spaces for alignment, I've found no practical way to enforce this via tooling/linting. And a rule without enforcement becomes inconsistent, which is how we get files full of mixtures of spaces and tabs, which is how people get frustrated with tabs, and we decide to throw it all out. And inevitably, that's part of how spaces "won" |
| |
| ▲ | throwawayqqq11 a day ago | parent | next [-] | | Or dont try to align lines with different indentation levels. Addind a comment with the right amount of tabs as a table header and align all fields with spaces after the tabs would do the trick. | |
| ▲ | camel-cdr a day ago | parent | prev [-] | | Disallowing /[^\t]\t/ and /^ / is a good start. | | |
| ▲ | ytpete a day ago | parent [-] | | These regexes don't solve what I think is one of the major common problems/complaints though: using extra tabs past the logical indent point as a shortcut to avoid typing so many spaces for alignment purposes. To take this example from a sibling post: if (foo) {
» frobnicate(bar,
» ...........baz);
}
Many people will wind up doing this: if (foo) {
» frobnicate(bar,
» » » ...baz);
}
And then your alignment is all messed up if you have a different tabs setting.Checking for that requires something more like a linter with a detailed understanding of the syntax parse tree. | | |
| ▲ | mananaysiempre 21 hours ago | parent | next [-] | | There are degrees of “detailed”. For example, I considered writing a small Awk program to check C code in response to another poster’s complaint about lack of tooling, but then quickly came to the conclusion that, with C’s insistence that (say) /??/<newline>* is a valid comment starter, getting this exactly correct probably does need an actual lexer that would go character by character. That sounded like it wouldn’t fit in an HN comment, so I stopped there. (That said, that’s as far as you’d need to go in the majority of cases. A dishonorable mention is warranted for languages that use the same character as an operator and a paired delimiter simultaneously, that is C++, Java, C#, TypeScript, and Rust with their abuse of the less-than and greater-than symbols, because that would in fact require a parser. In C++ especially, you’ll need full semantic analysis with template expansion, name resolution, and consteval evaluation. Because C++.) Yet you probably don’t actually need to be that accurate, do you? The majority of syntax highlighters aren’t, and they are still useful. You can usually afford to say that code that perverse deserves to lose, and in return I expect you should be able to gain a fair amount of language independence, which could be worth the tradeoff. So instead of checking if things are aligned with what they should be, you would just check they are aligned with something, like a left word boundary preceded by a delimiter, and so on. I can already see unpleasant corner cases after thinking about it for a few minutes, but it doesn’t look hellish yet, it looks something like you could experiment with over a weekend to see if it was viable. | |
| ▲ | g-b-r a day ago | parent | prev [-] | | At some point you just have to flog people ;) Anyhow, if code reviewers always use tools that highlight the tabs during the reviews, there's a good chance to catch these things. Maybe you could also have the tab width set randomly at every review, to make these horrors stand out |
|
|
|
|
| ▲ | mananaysiempre a day ago | parent | prev | next [-] |
| That’s mostly editor braindamage (that has unfortunately leaked into some otherwise very good codebases, like LuaJIT). Indent things with tabs, align with spaces[1]: if (foo) {
» frobnicate(bar,
» ...........baz);
}
Both camps will hate you, but things will work just as they should.[1] https://www.emacswiki.org/emacs/SmartTabs |
| |
| ▲ | jahewson a day ago | parent | next [-] | | Actually for me this shows why tabs don’t deliver on their promise. As soon as the user’s tab size is small enough that baz doesn’t need to wrap, the user gets suboptimal formatting. As someone who prefers tabs of 2 and often views code authored with tabs of 4 I encounter this often. | | |
| ▲ | g-b-r a day ago | parent [-] | | Your team should settle on a maximum line length independent of which tab settings one has Of course that will always be a compromise, either people who use narrow tab widths or those who use wide ones will have a (slightly) suboptimal experience. |
| |
| ▲ | phplovesong 15 hours ago | parent | prev [-] | | Formatting by hand is probably only going to work for private hobby projects. In 99% of the other cases there needs to be a formatter that does the formatting. | | |
| ▲ | mananaysiempre 11 hours ago | parent [-] | | That feels both largely irrelevant and false? False, because there are plenty of large projects that do have a house style but don’t use a formatter at code submission to enforce it—even if you reject the Linux kernel as an atypical example, basically every piece of commercial software from 20 years ago also fits, simoly because both formatters and presubmit checks weren’t nearly as common, and there were some chonkers there (like, I don’t know, Windows XP). Irrelevant, because it’s perfectly possible for a formatter to follow this convention (gofmt does, for example). |
|
|
|
| ▲ | fsmv a day ago | parent | prev | next [-] |
| This problem is solved by gofmt because it automatically aligns with spaces after the tab so humans don't mess up the whitespace |
|
| ▲ | bigcojosh a day ago | parent | prev | next [-] |
| https://nick-gravgaard.com/elastic-tabstops/ |
| |
| ▲ | Arelius a day ago | parent [-] | | Great, so now I need a special editor plugin, and a compatible editor just to be able to properly view code? No wonder why spaces "won" |
|
|
| ▲ | phplovesong 16 hours ago | parent | prev | next [-] |
| Wheb there i a formatter you should not care. You can rewrite the code if it bothers you. |
|
| ▲ | AdamH12113 a day ago | parent | prev | next [-] |
| I use tabs for indentation and spacing for alignment. Tables should be aligned with spaces. A wrapped line can be tabbed up to the start of the previous line and then spaced for alignment. |
|
| ▲ | marssaxman a day ago | parent | prev | next [-] |
| tabs for indentation, spaces for alignment |
| |
| ▲ | skylurk a day ago | parent [-] | | Meanwhile, I'm trying to get away from languages where whitespace has semantics. | | |
| ▲ | PaulDavisThe1st a day ago | parent [-] | | It's not about semantics at all. whitespace has (virtually) no semantics in C or C++, but there are few programmers who would feel comfortable reading such code without the suggestive hinting that indentation provides. |
|
|
|
| ▲ | fmbb a day ago | parent | prev [-] |
| Only a goblin would align code. But if you must you can start with a new line and the right indent. |
| |
| ▲ | gte525u a day ago | parent [-] | | From my experience, it seems to be the people that learned PASCAL first for some reason. |
|