Tabs vs Spaces

code-editors

#21

so tabs are not bad
Spaces and tabs have their advantages and disadvantages that is necessary to know before choosing them, so everyone can choose spaces or tabs, paying attention to style of code that he/she prefer.
I prefer to use tabs. I am writing code that looks good with them and I have no problem with readability.


#22

Yep, like I said: the faster the Elixir community sets this as the standard, the less people will suffer :smiley:


#23

It was decided quite a while ago…

:slight_smile:


#24

I usually go with whatever the standard for that language seems to be, so tabs for C, 2 spaces for Elixir and Html, and 4 spaces for most other languages.


#25

I would comment thispoor Richard :slight_smile:


#26

Or, you could NOT write highly nested code :slight_smile:


#27

Old thread, but someone above linked to this link: https://www.emacswiki.org/emacs/TabsAreEvil

However at this section: https://www.emacswiki.org/emacs/TabsAreEvil#toc7

There is a better way: use tabs for expressing the indentation level and spaces for alignment.

int f(int x,
......int y) {
--->return g(x,
--->.........y);
}

This is precisely what I do almost everywhere (except elixir because friggin spaces are prevalent in elixir, so I follow the pattern, ugh). I’ve been a big pusher for this style for over 2 decades now. You have custom indention levels, proper alignment, everything.


#28

There is a better way: use tabs for expressing the indentation level and spaces for alignment

Yeah it’s what I usually do… I don’t see why others don’t. Each user can us their preferred indentation size and everything still always aligns as the writer intended.

I of course write code in other people projects as they prefer the code to be. But in my house we use tabs for indentation and spaces for alignment.


#29

Do you mean use the tab KEY to step to the right indentation level but have space characters in the file then I agree. If you mean have tab characters in the actual file then I disagree. Having tab chars only works if everyone can agree on the size of a tab otherwise it totally screws things up.

All you need to do is to set indent-tabs-mode to nil and you are set. :wink:


#30

How does it screw everything up?
If the tab CHAR is used for indentation but with added spaces the few times some alignment is needed then the visual impact of the code is the same for everyone no matter what they have have tab spacing set to. Giving the each person the ability to have a preference without it ever affecting others in any way and still everyone will see the code correctly aligned as intended.

If you look at the example OvermindDL1 gave just above then this code will be the same everywhere. x will always align with y wether or not the viewer has his tabs size set to 4, 2 or 11 spaces.


#31

Tab as indention and space as alignment always works perfectly, and each use can set their own tab size and it auto-adjusts as expected, such as in my prior quoted example:

// 4-tabstop
int f(int x,
......int y) {
--->return g(x,
--->.........y);
}


// 2-tabstop
int f(int x,
......int y) {
->return g(x,
->.........y);
}


// 8-tabstop
int f(int x,
......int y) {
------->return g(x,
------->.........y);
}

So the int y is always aligned, since spaces are used for alignment, the indention of the return block (and its after-aligned y) adjust as expected, etc… Been programming this way for almost two decades.


#32

Yes, but if it to look like you intended then the size of the tabstop is critical. I mean if you assume the tabstop is 2 characters and I view it with tabstops of 8 then the appearance is very different. Also you are assuming that you start the indentation with tabs and then extend it with spaces. Mixing tabs and spaces does not work.

I still think the easiest and most consistent is to always use spaces in the file and have a smart tab key which knows about indentation and goes to the right place.


#33

To me the ‘intended size’ can be anything, that is why I only use tabs for indention, sometimes I may view them at 8-tabstop, sometimes 2, even I change on occasion, with using that pattern it just ‘always works’ without needing to reformat the code. ^.^

And yes, start indentation with tabs, then do alignment with spaces. Tabs will only ever exist at the very start of a line with a count of 0-to-N, never elsewhere.

That is horror on formatting though. What about a project that uses 8-spaces as their tabs and you want to print it (manager stupidity or something), or what if a project uses 2 but you’ve not had coffee yet and want to view it with more? Smart tab keys are fine for putting ‘in’ auto-spaced indentation, but it does nothing for viewing existing files. Using a mix of tabs and spaces is forward thinking as it allows you to both put in with ease and view an arbitrary-period-of-time-in-the-future with ease on whatever display you may have then. :slight_smile:


#34
// 4-tabstop
int f(int x,     // line 1
......int y) {   // line 2
--->return g(x,  // line 3
--->.........y); // line 4
}

When discussing functional programming the “code that is easier to reason about” argument invariably surfaces - on the flip side when writing code I don’t want to have to waste any brain cycles to determine where indentation stops and alignment starts (e.g. suppress the urge to use the tab key on line 2 and to use more spaces than my tab width to ensure proper alignment on line 4). I guess this type of formatting could be left to the editor - if one can be bothered to getting it properly configured for such a task.

There is more to coding styles than just indentation - maybe I find it easier to read code like this

int f(
    int x,
    int y
) {
    return g(
        x,
        y
    );
}

Ultimately we can’t escape the author’s/organization’s coding style short of reformatting the code either automatically or manually - and often there are far greater obstacles to code readability than just tab-size.

This topic hasn’t referenced this yet:
400,000 GitHub repositories, 1 billion files, 14 terabytes of code: Spaces or Tabs?

According to the article on github spaces win over tabs - not that majorities are always right (they are just an indication of what’s popular) but it certainly lines up with the notion that with “just spaces” there is less to keep track of.

Interestingly github lets you control your displayed tab-size with a ts query string parameter, e.g.:

example

Of course this only works if the project actually uses tabs.


#35

Yep yep, I’m lazy so that is what I do. ^.^

For Elixir work I often still use Atom, which does not have a good plugin for that, but for Elixir I keep to 2-spaces for indentation regardless in all code as that is part of the style of the language.

Huh, I’d not known about that, awesome!


#36

Computer programmers who use spaces as part of their coding earn $15,370 (£12,000) more per year than those who use tabs, a survey of developers has revealed.

The survey found the salary difference stretched across different languages, countries and experience levels.

The debate over whether it is better to use spaces or tabs to indent code has raged among programmers for years.

Indents act like paragraph markers and help define how programs work.

The result was “surprising,” said David Robinson, data scientist at Stack Overflow which carried out the survey of 12,400 developers.

:043:


#37

That seems to be based on the languages chosen. Hacker News went wild peeling it apart. :wink:

A notice was that people that used just spaces often cared less about typography and thus things often looked worse too. :wink:


#38

I always use spaces.

My EditorConfig:

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

#39

I tend to use spaces, because I have much more control over how it will look like for others.

I do use tabs though where I have to, be it because its considered more idiomatic (go), because they are needed to actually work (GNU Makefile) or just because they are corporate policy (complicated[*]).

[*] In my current job we prefer 2 spaces, but since we also use go and GNU Make, we use a tabwidth of 4 there and also we use a CI tool which ensures whitespace consistency.


#40

I have

(custom-set-variables
  '(indent-tabs-mode nil)
  ...                                 ;Lots of good stuff
  )