Tabs let you define how big you want each indent to be
…except when they don’t. Many common environments have a hardcoded tab size of 8, which is insanely big for using it for indentation.
Because other people might have restricted environment which might not suit their preference is not a good reason to level it down IMO.
Also, I think 9 is the best size for indent (matter of preference), do you think I should switch to space so everyone can enjoy this wonderful view I have ?
It’s not just “might”. Termux is pretty much the only good choice for programming on Android.
I think 9 is the best size for indent (matter of preference), do you think I should switch to space
I think you should switch to an exorcist.
What’s your point ? You can use vim on termux and set the tabsize to whatever you want for example.
This is the biggest problem with tabs. Too many tools don’t let you adjust the size (or make it very difficult). This is the only reason I usually prefer spaces (only very slightly).
My dream solution is elastic tabstops and I’ve posted about it here before a few months ago. The problem with wanting elastic tabstops is that it seriously compounds the issue of “editors don’t properly support it”
What environment are you using that has a hardcoded tab size? I haven’t seen this since typewriters.
Some projects just use tabs as a compressed form of 8 spaces. But that is a sin. Use tab to mean “one indent level” and align with spaces if you need to. (the occasional ASCII art diagram)
What environment are you using that has a hardcoded tab size?
- Termux
- SourceHut
- “View page source” in the browser
Termux
I think running tabs -N
(where N
is you preferred tab size) in the terminal should work. This is what I use in my zshrc on desktop.
SourceHut
Yup, they seem to be pretty opinionated here. If you look at the source there is just an inlined style with a single rule pre { tab-size: 8 }
. I guess that is what you get when you use opinionated tools. The user’s browser isn’t right, my preference is right!
“View page source” in the browser
On Firefox this uses my default tab size of 4. But I guess changing this default isn’t user-friendly.
As an embedded software developer that does linux kernel drivers I’ve come to love the tab size 8 indentation level.
I’m paraphrasing: “if your indentation level gets too deep, it’s time to rethink/refactor your function.”
And with tab 8 you’ll notice it rather quick if your function does too much/unrelated stuff.
A function should be short and do one thing only, if possible. It also makes unit testing easier if that’s a requirement.
When you’re operating on such a low level of abstraction, it’s no wonder you don’t need deep nesting.
Oh, I’ve done my fair share of C++ and Python as well. But you got to agree with me that when you are on your fourth indented “if case” it’s time to step back and think about what you are trying to achieve. I mean it’s probably going to work, but probably also very hard to maintain that type of code.
I consider tabs for indentation a failed concept.
The idea is good, but it evidently failed. Most guidelines and newer Tools recommend or require or use spaces for indent. They have their reasons too.
The prevalence of spaces makes it hard to make a contrary argument for tabs. By now, I don’t think it’s worth even if it had reasonable advantages.
Editors/IDEs that parse syntax can adjust space indent too. A mixture for indent and alignment is not obvious for everyone (I always display whitespace in my editors and am deliberate and consistent, but many people and editor defaults won’t be). Some defaults of four or eight space-width tab display is atrociously wasteful and inaccessible.
Spaces are a good enough baseline. It works well enough. And most importantly it works consistently. That’s why it won in prevalence and use.
Do you have actual experience with it?
Some time ago I read a comment from someone with impaired eyesight who used an editor that would adjust space indent just fine.
Accessibility is a thing I always consider.
If I could only get everyone who works on the thing I work on to use a whitespace visualizer, it would be enough. We can fight about tabs or spaces after we get rid of all the unnecessary trailing ones.
In case folks aren’t aware, https://editorconfig.org/
Wrong.
Tabs for indent, spaces for alignment. This is the way, I can’t believe people are still fighting that ?
Then you lose the benefit of tabs: you can’t adjust the tab width without destroying alignment. So you end up with a confusing mix of characters for no benefit.
Mixing them is the worst option.
You might not understand how to do it properly so here’s the idea:
Tabs will let you reach the indentation level of the current block, then from here, you’ll use spaces to align stuff property. Here’s an example, where •••
are tabs (I’m exaggerating alignment for the sake of the example) :
>•••if (condition1 == true
>••• || condition2 != false)
>•••{
>•••>•••struct ident people[] = [
>•••>•••>•••{
>•••>•••>•••>•••.name = "bob",
>•••>•••>•••>•••.pubkey = "value1",
>•••>•••>•••},
>•••>•••>•••{
>•••>•••>•••>•••.name = "alice",
>•••>•••>•••>•••.pubkey = "value2",
>•••>•••>•••}
>•••>•••];
>•••>•••secureConnection(people[0].name, people[0].pubkey,
>•••>••• people[1].name, people[1].pubkey,
>•••>••• CRYPTO_ALGO_DEFAULT);
>•••}
As you can see, everything will stay correctly aligned as long as it’s within the same block.
You’re confusing using tabs for indentation and spaces for alignment with using tabs and spaces for indentation. This means each line starts with tabs. Next you optionally have spaces for alignment with previous lines. Then you have content (like code or comments). Because you never have a tab following a space the alignment is never destroyed by adjusting how wide a tabstop is.
I am not, it’s easy to find examples where tabs first then spaces breaks down.
The opposite is true, though. If you use tabs for indentation and spaces for alignment, you can adjust the tab width without destroying alignment. That’s the big benefit of the tabs-for-indentation-spaces-for-alignment mix.
You can’t do that with only tab characters, you can’t even align stuff with tabs because it has variable width.
You can preserve alignment in a mixed context, but it complicates things.
Anything for indent (barely matters, as long as the editor forces it to stay consistent), and fuck alignment, just put things on a new line.
struct Ident arr = [
{
.id
= 0,
.name
= "Bob",
.pubkey
= "",
.privkey
= ""
},
{
.id
= 1,
.name
= "Alice",
.pubkey
= "",
.privkey
= ""
}
];
Not like that, lol
Just saying, instead of this monstrosity
CreateOrderRequest(user,
productDetails,
pricingCalculator,
order => order.internalNumber)
Just use
CreateOrderRequest(
user,
...
Putting the first argument on a separate line.
Same if you have an if
using a bunch of and
(one condition per line, first one on a new line instead of same line as the if
) and similar situations.
seconded on not aligning things. its the whole source of the problem in the first place and doesnt even serve a purpose
I used to think this way, at least when writing C++. But it’s objectively harder to do and convince other people to follow, especially if they can’t be bothered to change their environment to display tabs and spaces differently. It’s a losing battle so now I just do spaces when working with other people
Always do spaces, because you can never trust how someone else has their tab configured.
How is this even a debate anymore. I thought we all agreed on this years ago.
you can never trust how someone else has their tab configured
Why on earth would I care how someone else has their editor configured? It’s none of my business, and none of yours either.
I’ve yet to find tooling that supports this. Clang format has a setting that looks like it does it, but actually does something else. If I have to press the spacebar a bunch of times each time I add an argument to a function, that’s a pain, and it’s a bigger pain to convince the people I’m working with that that pain’s less bad than using spaces everywhere and having the IDE deal with it.
Until the people making editors and auto formatters acknowledge that the obvious most sensible whitespace style is even a thing, I’m forced to do something else and be really grumpy about it.
I understand your point of view. Personally I either copy the previous line and replace the arguments there, or insert X number of space using the repetition feature of my editor. It also has a feature that will align multiple cursors together with the “farthest” one using space, which is a killer feature for me! (See this presentation video @1:40).
It’s hard to do this consistently (especially in a team) because people might (and statistically in a large enough project, will) use the tab key for alignment since it’s faster than pressing space, or just be confused about what whitespace is tabs and what is space. Just using space everywhere is idiot proof and requires no work to micromanage. The only way to use tabs is to not align at all.
I agree that it’s hard, but not impossible. This usually boils down to how Nazi people are when merging code. In a corporate environment, nobody gives a damn so yeah you gotta use whatever you want because there are already different indentation systems within the same file anyway :)
But hey, you gotta live by the changes you want to see happen, so I personally put a lot of effort in formatting my code regardless.