I wish every language had a gofmt, this is such a non-debate (tabs are indentation, spaces are alignment)
Tabs are objectively better than spaces - gomakethings.com
Submitted 1 year ago by mac@programming.dev to programming@programming.dev
https://gomakethings.com/tabs-are-objectively-better-than-spaces/
Comments
corytheboyd@kbin.social 1 year ago
Asudox@lemmy.world 1 year ago
rustfmt
sekhat@lemmy.temporus.me 1 year ago
And over gofmt, rustfmt lets you set settings for the project. Keeps the code looking how I want, and contributers don’t have to care.
zygo_histo_morpheus@programming.dev 1 year ago
Another accessibility reason for tabs: when using a braille display, each space takes up one character cell, so indenting with four spaces eats up four cells. Indenting three times with four spaces each eats up 12 characters already. Tabs only take one character cell each, so three indents = three character cells used.
The fact that there (I assume?) isn’t a braille oriented text editor that can handle space-based indentation in a smarter way is a bit depressing. Maybe the solution should be better tools based around accessibility rather than convincing everyone to switch to tabs, which is a project that will just never succeed.
atheken@programming.dev 1 year ago
So your fix is “convince all the people that want/need the better handling to use a specific editor?” - perhaps it’s a smaller number of people, but do you not see the irony there?
I honestly don’t care about tabs vs. spaces, but if there’s a low cost change in my setup that makes it easier on others, why not?
zygo_histo_morpheus@programming.dev 1 year ago
My spontaneous reaction is that making some sort of braille oriented setting for some or hopefully most editors used by people with braille displays (I have no idea if using a “normal” editor even makes sense if you’re using a braille display) is the most pragmatic solution to their screens being taking up by spaces.
First of all, convincing everyone to use tabs is a monumental task. Convincing people with braille displays to use more convenient tools on the other hand seems pretty easy, why wouldn’t you want to use more convenient tools?
Secondly, there is a large amount of code written with spaces today, so even if people switch with tabs in the future you might still want to be able to read legacy code.
Thirdly, I don’t think that the choice of tabs vs. spaces is completely arbitrary because of alignment. Using tabs for indentation and space for alignment leads to a lot more micro management of whitespace compared to just using spaces. I would guess that alignment isn’t very braille friendly anyway, but it does make the code more readable for other people. Having a good braille editor affordance might be closer to letting us have our cake and eat it too.
Of course, I don’t know what this would look like exactly, and maybe there’s some sort of obstacle that I’m overlooking, I do want to be clear that this is just of the top of my head as someone who has never used a braille display.
ck_@discuss.tchncs.de 1 year ago
This is the attitude that’s needed 👍
ck_@discuss.tchncs.de 1 year ago
rather than convincing everyone to switch to tabs, which is a project that will just never succeed.
Few years back, Coraline Ada Ehmke went on a one person crusade opening a pull request on every major Github repository to adopt a code of conduct for the project, detailing the complex rules of how the humans in that microcosm of a project should interact with one another. Today, it’s the norm.
Arguing that it’s invincible to convince people at large to adopt tabs over spaces with good arguments is a ridiculous statement. All you are doing is making up excuses for not having to care.
zygo_histo_morpheus@programming.dev 1 year ago
Arguing that it’s invincible to convince people at large to adopt tabs over spaces with good arguments is a ridiculous statement
I do actually think that it is very hard to convince basically every programmer of something, no matter how good arguments you have.
Also, without knowing much about the issue, it sounds a bit like the tooling for people using braille displays isn’t very good and fixing that is maybe also worth advocating for, perhaps it’s even a strategy for advocacy that is more effective?
TheCee@programming.dev 1 year ago
That reminds me of those times when back on reddit some dev showed up to present their new GUI library. Bragging about how they were better than Qt devs etc. (even though they didn’t implement the hard parts, like working text fields or tables)
After some time a bunch of people had enough and started bullying those guys into submission about accessibility. After some time, every of those toolkits had support or at least plans for supporting screenreaders. Eventually, AccessKit became a thing.
Good times.
pexavc@lemmy.world 1 year ago
Yeah. It is depressing.
I’ve always wanted an accessibility feature that uses haptic feedback to mimic braille patterns for reading purposes too.
In general a lot of creative stuff can be done if we focused on it even a tiny bit more.
wgs@lemmy.sdf.org 1 year ago
Tabs for indent, spaces for alignment. This is the way, I can’t believe people are still fighting that ?
realharo@lemm.ee 1 year ago
Anything for indent, and fuck alignment, just put things on a new line.
wgs@lemmy.sdf.org 1 year ago
Like that ?
struct Ident arr = [ { .id = 0, .name = "Bob", .pubkey = "", .privkey = "" }, { .id = 1, name = "Alice", .pubkey = "", .privkey = "" } ]
Man you’re a genius ;)
milo128@lemm.ee 1 year ago
seconded on not aligning things. its the whole source of the problem in the first place and doesnt even serve a purpose
GuybrushThreepwo0d@programming.dev 1 year ago
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
KIM_JONG@lemmy.world 1 year ago
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.
AnyOldName3@lemmy.world 1 year ago
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.
wgs@lemmy.sdf.org 1 year ago
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).
zagaberoo@beehaw.org 1 year ago
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.
wgs@lemmy.sdf.org 1 year ago
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.
JackbyDev@programming.dev 1 year ago
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.
zygo_histo_morpheus@programming.dev 1 year ago
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.
jsnfwlr@lemmy.world 1 year ago
And this is why language servers and formatters are so critical.
wgs@lemmy.sdf.org 1 year ago
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.
eee@lemm.ee 1 year ago
Neither tabs or spaces are good. The correct way is to leave no whitespace in the code at all. It’s unnecessary and adds to processing time.
Everyone should aim for 1LOC per commit
Dr_Cog@mander.xyz 1 year ago
“Error: syntax error on line 1”
…shit
eee@lemm.ee 1 year ago
Great, no scrolling through thousands of lines to find the right one!
Dark_Arc@social.packetloss.gg 1 year ago
My program, written in the whitespace language, ruined.
CURSE YOU PERRY THE PLATYPUS!
MajorHavoc@lemmy.world 1 year ago
Now next time I read anything about why any Python libraries are named what they are named, I’m going to hear Dr Doofenschmirtz voice. Thank you for that.
shotgun_crab@lemmy.world 1 year ago
Code formatter
_stranger_@lemmy.world 1 year ago
Holy wars are fun, but this is the answer.
At least this way the holy war takes place in commits to change the formatting file.
stevecrox@kbin.social 1 year ago
Parameter declarations, array initialisation and lambda alignment is the most common.
Java Lambda expressions can be hundreds of characters long so wrap on to the next line. A lot of code formatters will auto wrap with a single identation.
A lot of developers instead like to align each lambda call with the one above. They feel its more readable, personally I agree.
Normally in DevSecOps I offer a standard code format to a team, I highlight the contentious choices (like spaces or tabs) and ask the team if they have strong feelings about it and then sit back and let them decide.
stevecrox@kbin.social 1 year ago
Years ago there was no way to share IDE settings between developers.
You ended up with some developers choosing a tab width of 2 spaces, some choosing 4 spaces and as there was no linting enforcement some people using 2-4 spaces depending on their IDE settings.
This resulted in an unreadable mess as stuff was idented to all sorts of random levels.
It doesn't matter if you use tabs or spaces as long as only one type is consistently used within a project.
Spaces tends to win because inevitably there are times you need to use spaces and so its difficult to ensure a project only uses tabs for identation.
IDE's support converting tabs into spaces based on tab width and code formatting will ensure correct indentation. You can now have centralised IDE settings so everyone gets the same setup.
Honestly 99% of people don't care about formatting (they only care when consistency isn't enforced and code is hard to read), there is always one person who wants a 60 charracter line width or only tabs or double new lined parathensis.
FlagonOfMe@sh.itjust.works 1 year ago
inevitably there are times you need to use spaces
When? You indent with tabs then add any spaces you want for precise alignment. When would you need to use spaces to indent?
stevecrox@kbin.social 1 year ago
Parameter declarations, array initialisation and lambda alignment is the most common.
Java Lambda expressions can be hundreds of characters long so wrap on to the next line. A lot of code formatters will auto wrap with a single identation.
A lot of developers instead like to align each lambda call with the one above. They feel its more readable, personally I agree.
Normally in DevSecOps I offer a standard code format to a team, I highlight the contentious choices (like spaces or tabs) and ask the team if they have strong feelings about it and then sit back and let them decide.
My only thing is ensuring you don't mix tabs and spaces.
shagie@programming.dev 1 year ago
Editor Config ( editorconfig.org ) is one of the first files I check into a repo and is there to make sure that everything is done consistently between all editors - IntelliJ, Notepad++, VS Code…
Saganaki@lemmy.one 1 year ago
Tabs for indentation/increased scope, spaces for alignment. The best answer.
_stranger_@lemmy.world 1 year ago
Soft tabs are superior. press tab-> get 4 spaces.
Saganaki@lemmy.one 1 year ago
…did you not read the article?
LetterboxPancake@sh.itjust.works 1 year ago
In come my idiotic colleagues:
“buT w3 lIke 2 SpACe5!”
fuck with the linter
darq@kbin.social 1 year ago
I've always wondered why some people tout "forcing a consistent appearance across environments" as a pro for spaces. That's a bad thing.
To be honest I'm surprised code format converters aren't ubiquitous. Let the repo have it's master format, enforced on commit. Then converters translate into each developer's preferred standard dialect on checkout and back again on commit.
Duralf@lemmy.world 1 year ago
This is a holy war that I will gladly fight again and again! I can’t believe that soft tabs are more popular, especially in python!
_stranger_@lemmy.world 1 year ago
That is because they are superior.
spatium vult.
Whisp@kbin.social 1 year ago
Python noob here. I thought you were only supposed to use spaces with python and tabs would give you indentation errors?
UlrikHD@programming.dev 1 year ago
Tabs work fine, you aren’t allowed to mix, indentation must be consistent.
Lucky@programming.dev 1 year ago
The argument for having tabs adjust depending on your ide sounds better than it is in practice. Someone formatting code to look nice with width 4 will look horrendous for someone who uses width 8.
Spaces makes it uniform and captures the exact style the original dev intended
CodeMonkey@programming.dev 1 year ago
If you have your tab width set on 8, that is on you. You will also set your IDE to insert 8 spaces when you press TAB and I will cry when I have to give you a code review.
When I indent my code, I am indicating that I am in a nested block. I don’t care if, on your screen, that indent is 2, 3, or 4 characters.
Acters@lemmy.world 1 year ago
Anarchist: tab with set to 1
ck_@discuss.tchncs.de 1 year ago
If the original dev intended to make their code less accessible and their project less inclusive, they should rethink their priorities.
mckean@programming.dev 1 year ago
I think calling one way better than the other is flawed. The reason the title is saying that tabs are objectively better is because they are used in addition to where spaces are used elsewhere. You could make the same argument in favor spaces due to keeping things simpler.
The argument of having variable indent size for tabs so viewers can decide how big they are is imho legitimate but also not the goal as it’s addressing something that teams generally agree on. There is max characters per line, brace placement, general code style and rules. Yes we can eject the indentation from the rules that are agreed on but once again simplicity over complexity has an equal say.
In the end it doesn’t matter that much, a good programmer will be able to work in either setting, the Editor will do most of the work anyways.
With all that said, spaces all the way!
Asudox@lemmy.world 1 year ago
Tabs take less space (space as in space in storage, like free as in freedom) tbh.
AnUnusualRelic@lemmy.world 1 year ago
My disk is almost full! Better switch to tabs for indentation!
Asudox@lemmy.world 1 year ago
Happened to me quite a few times, relatable situation.
Magnetar@feddit.de 1 year ago
Wrong.
ck_@discuss.tchncs.de 1 year ago
The choice of having tabs over spaces is essentially the same as putting up wheelchair ramp / elevators, tactile pavement, etc. or not.
You either care to make life easier for people with disabilities to participate in this particular form of social interaction or you don’t give a fuck because “you don’t like it”.
redempt@lemmy.world 1 year ago
why don’t we store code unformatted and have everybody’s IDE display it with their preferred format applied? it would make everything easier and stop people bickering over pointless things.
Ashtefere@lemmy.frozeninferno.xyz 1 year ago
That’s what tabs are for. 1 tab, to an ide, means “you choose how many spaces this tab is, and when we commit it back to git it won’t fuck the history up.”
maor@lemmy.org.il 1 year ago
I guess they were referring to formatting other than tabs, like place of brackets and line length, which sounds like a neat idea
moomoomoo309@programming.dev 1 year ago
Storing an AST would be interesting, but it’d require the IDE to support parsing each specific language, so you’d probably want something like an LSP but for just parsing to handle that.
Sigmatics@lemmy.ca 1 year ago
Seems there’s not much else going on
skulbuny@sh.itjust.works 1 year ago
laughs in lisp
Nah, I’ll keep on sticking with spaces or whatever the language’s formatter uses. Ain’t no way am I mixing tabs and spaces, will just stick with spaces.
sekhat@lemmy.temporus.me 1 year ago
I used to be a tabs guy, somepoint over time, especially when I realized some of the edge cases I have in formatting only remain consistent when using spaces, I switched.
Phen@lemmy.eco.br 1 year ago
Looks like you missed the point. This is about indentation, not formatting.
Use tabs to indent your lines, but if you want to align a parameter with the parenthesis on the line above or something like that, you add spaces after the indentation tabs.
That way if someone wants to they can configure their tab length to 20 spaces and the indentation will remain consistent and the code will remain aligned.
Vince@feddit.de 1 year ago
I thought it was a non-issue that tooling should take care of anyway until stackoverflow published this:
stackoverflow.blog/…/developers-use-spaces-make-m…
So spaces all the way
potterman28wxcv@beehaw.org 1 year ago
Make sure to specify you only use spaces before asking for the raise! It works!
potterman28wxcv@beehaw.org 1 year ago
Interesting take. I prefer spaces because each piece of code that I see with tabs has an implicit tabsize you really need to have if you don’t want the code to look ugly - especially if the person has been mixing tabs and spaces - and they usually do. Sometimes unadvertently.
When you remove all tabs at least everyone is on the same page.
To the actual problem raised by the article:
I have ADHD. Two spaces per indent makes it damn near impossible for me to scan code. My brain gets too distracted by the visual noise. Someone who’s visually impaired might bump their font size up really large, and need to scale up or down the amount of space per indent. Someone might just prefer it because…
I wonder if it could be possible to make a code editor that would allow for adjusting the “indent number of spaces you see”. Code editors are able to figure out what are indents and what are not, so in theory it should be possible
Ashtefere@lemmy.frozeninferno.xyz 1 year ago
Yes. That’s what tabs are for. You can choose the width of the tab. It can be small for people with small screens. It can be big for this guy or people with 600 inch ultrawides.
Tabs are objectively superior because they are exactly what everyone wants at all times, and the git commit history does not get polluted.
SleveMcDichael@programming.dev 1 year ago
Tabs let you define how big you want each indent to be, and spaces do not.
Spaces can too: Simply use more or less of them, to taste.
I have ADHD. Two spaces per indent makes it damn near impossible for me to scan code.
Then use four, or six, or eight, or 20. Hell, most code I’ve seen uses four spaces per indent anyway.
[Re: braille]
Surely there’s an editor out there that will automatically display indent spaces as a tab character. Or failing that it seems like it would be rather trivial create a program to convert n spaces to tabs, and vice versa.
spiderplant@lemm.ee 1 year ago
Spaces do not allow the viewer of code to choose how wide the indents are, this is dictated by the developer.
Most IDEs allow users to customise how many spaces to display tab indents as. Doing so the other way around may cause issues with languages based on whitespaces such as python.
vzq@lemmy.blahaj.zone 1 year ago
That’s all well and good if everyone uses editors you can configure to a certain standard all the time. Then tabs all the way.
Unfortunately that’s not reality for everyone.
sirdorius@programming.dev 1 year ago
Honestly, what fucking editor are you using that doesn’t allow you to configure tab length? Wordpad?
vzq@lemmy.blahaj.zone 1 year ago
I’m hopping between shells using whatever is installed.
jelloeater85@lemmy.world 1 year ago
TheCee@programming.dev 1 year ago
Original poster is right by all accounts, of course. Now, let’s come up with exotic significant indentations.
function xyz(a, b): | var x = 2 | if true: | | do_something() | else: | | do_something_else() | anyway()
Pro: Your editor no longer needs to implement indentation hints.
Con: Looks obstructive if not highlighted like an indentation hint.
30p87@feddit.de 1 year ago
Ah yes I understand now!
ono@lemmy.ca 1 year ago
insomniac_lemon@kbin.social 1 year ago
I'm tinkering with a whitespace language and prefer using 1-space rather than 2. I don't really like the double character for 1 level. Is that weird?
Tabs are forbidden though I could use tabs with a built-in code filter for the compiler to turn 1 tab into 2 spaces, and that might be easier when working with others (though I don't know how it would be seen, especially needing to change editor tab behavior).
dukk@programming.dev 1 year ago
Honestly, my editor (Neovim) just picks between tabs and spaces for me, so I just end up using whatever’s already there. The only language where I’ll explicitly use one is Haskell, just because spaces there allow me to keep everything nice and lined up.
xigoi@lemmy.sdf.org 1 year ago
…except when they don’t. Many common environments have a hardcoded tab size of 8, which is insanely big for using it for indentation.
wgs@lemmy.sdf.org 1 year ago
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 ?
agitated_judge@sh.itjust.works 1 year ago
Ah, the best kind of indent. A tab and a space.
xigoi@lemmy.sdf.org 1 year ago
It’s not just “might”. Termux is pretty much the only good choice for programming on Android.
I think you should switch to an exorcist.
icesentry@programming.dev 1 year ago
Why would you ever need 9 other than trolling people on the internet?
JackbyDev@programming.dev 1 year ago
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”
nickgravgaard.com/elastic-tabstops/