this post was submitted on 05 Sep 2023
166 points (79.6% liked)

Programming

17398 readers
107 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 49 points 1 year ago (4 children)

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.

[–] [email protected] 43 points 1 year ago (4 children)

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 ?

[–] [email protected] 17 points 1 year ago (1 children)

Ah, the best kind of indent. A tab and a space.

[–] [email protected] 9 points 1 year ago (2 children)

Or just set tabsize to 9, that's the point :)

load more comments (2 replies)
[–] [email protected] 6 points 1 year ago (4 children)

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.

load more comments (4 replies)
load more comments (2 replies)
[–] [email protected] 20 points 1 year ago (3 children)

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)

[–] [email protected] 7 points 1 year ago (1 children)

What environment are you using that has a hardcoded tab size?

  • Termux
  • SourceHut
  • “View page source” in the browser
[–] [email protected] 15 points 1 year ago* (last edited 1 year ago) (5 children)

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.

load more comments (5 replies)
load more comments (2 replies)
[–] [email protected] 13 points 1 year ago (6 children)

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.

load more comments (6 replies)
[–] [email protected] 10 points 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"

https://nickgravgaard.com/elastic-tabstops/

[–] [email protected] 38 points 1 year ago (3 children)

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.

[–] [email protected] 12 points 1 year ago (2 children)

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?

[–] [email protected] 15 points 1 year ago (10 children)

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.

load more comments (10 replies)
load more comments (1 replies)
[–] [email protected] 7 points 1 year ago (2 children)

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.

load more comments (2 replies)
load more comments (1 replies)
[–] [email protected] 38 points 1 year ago (4 children)

I wish every language had a gofmt, this is such a non-debate (tabs are indentation, spaces are alignment)

load more comments (4 replies)
[–] [email protected] 37 points 1 year ago (2 children)

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

[–] [email protected] 16 points 1 year ago (1 children)

"Error: syntax error on line 1"

...shit

[–] [email protected] 8 points 1 year ago (1 children)

Great, no scrolling through thousands of lines to find the right one!

load more comments (1 replies)
[–] [email protected] 11 points 1 year ago (1 children)

My program, written in the whitespace language, ruined.

CURSE YOU PERRY THE PLATYPUS!

load more comments (1 replies)
[–] [email protected] 34 points 1 year ago* (last edited 1 year ago) (2 children)

Code formatter and run auto format

load more comments (2 replies)
[–] [email protected] 31 points 1 year ago (17 children)

Tabs for indent, spaces for alignment. This is the way, I can't believe people are still fighting that ?

[–] [email protected] 29 points 1 year ago* (last edited 1 year ago) (3 children)

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.

[–] [email protected] 16 points 1 year ago* (last edited 1 year ago) (3 children)
struct Ident arr = [
{
.id
= 0,
.name
= "Bob",
.pubkey
= "",
.privkey
= ""
},
{
.id
= 1,
.name
= "Alice",
.pubkey
= "",
.privkey
= ""
}
];
[–] [email protected] 17 points 1 year ago* (last edited 1 year ago) (8 children)

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.

[–] [email protected] 8 points 1 year ago

People seem to have a real issue with using new lines and I've never quite understod why.

It feels like a lot of those people are using notepad like applications instead of coding focused ones with collapsible regions etc.

load more comments (7 replies)
load more comments (2 replies)
load more comments (2 replies)
[–] [email protected] 9 points 1 year ago (7 children)

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

load more comments (7 replies)
load more comments (15 replies)
[–] [email protected] 22 points 1 year ago* (last edited 1 year ago) (4 children)

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. Who then sucks up huge amounts of the team time arguing their thing is a must while they code in emacs, unlike the rest of the team using an actual ide.

load more comments (4 replies)
[–] [email protected] 19 points 1 year ago (1 children)

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.

[–] [email protected] 8 points 1 year ago (2 children)

The consistent appearance thing is probably more about how mixing tabs (for indentation) and spaces (for alignment, eg in multi-line function definitions of calls) looks like complete crap if you change the tab width.

[–] [email protected] 7 points 1 year ago (5 children)

I think you have it backwards. If you use tabs for indentation and spaces for alignment it works great for any tab size.

It is when you use a tab just as a compressed representation of 8 spaces and use them for alignment as well that it goes to shit. (because you have made the sin of tab == 8 spaces instead of the correct tab = 1 indent level)

load more comments (5 replies)
[–] [email protected] 7 points 1 year ago* (last edited 1 year ago)

Using only tabs for indentation and only spaces for alignment will never result in crap alignment when adjusting tabstops because the alignment does not use tabs.

This is using both tabs and spaces for alignment.

--->func foo(int i,
--->--->     int j);

Observe what adjusting the tabs does,

->func foo(int i,
->->     int j);

This uses only spaces for alignment,

--->func foo(int i,
--->         int j);

When converted the alignment is maintained because the tabstops aren't used for alignment, only for indentation.

->func foo(int i,
->         int j);
[–] [email protected] 18 points 1 year ago

Yesterday, I shared some spicy takes. A few were particularly controversial—most notably, that I correct Gif the correct way (with a soft G)

And I stopped reading there.

[–] [email protected] 17 points 1 year ago (1 children)

Tabs for indentation/increased scope, spaces for alignment. The best answer.

[–] [email protected] 8 points 1 year ago (4 children)

Soft tabs are superior. press tab-> get 4 spaces.

[–] [email protected] 9 points 1 year ago

…did you not read the article?

load more comments (3 replies)
[–] [email protected] 15 points 1 year ago (9 children)

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.

load more comments (9 replies)
[–] [email protected] 10 points 1 year ago (2 children)

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

[–] [email protected] 11 points 1 year ago (2 children)

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.

load more comments (2 replies)
load more comments (1 replies)
[–] [email protected] 10 points 1 year ago (4 children)

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!

load more comments (4 replies)
[–] [email protected] 9 points 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!

[–] [email protected] 8 points 1 year ago (1 children)

Tabs take less space (space as in space in storage, like free as in freedom) tbh.

[–] [email protected] 8 points 1 year ago (1 children)

My disk is almost full! Better switch to tabs for indentation!

load more comments (1 replies)
[–] [email protected] 7 points 1 year ago

Seems there's not much else going on

load more comments
view more: next ›