Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldn't I mix tabs and spaces?

I often read that I shouldn't mix tabs and spaces in Haskell, or that I shouldn't use tabs at all. Why?

like image 362
Zeta Avatar asked Mar 07 '16 22:03

Zeta


People also ask

Can you mix tabs and spaces?

Specifically, mixing tabs and spaces in the same block is illegal, but blocks with spaces and blocks with tabs are allowed in the same file.

Why programmers who use spaces instead of tabs?

Their research found that spaces were far better for a number of different reasons. Not only is this technique more visually appealing, it allows programmers to make more money. The analysis performed by the team at Stack Overflow found that programmers who use spaces instead of tabs are making more money.

Should I indent with tabs or spaces?

So, at the end of the day, tabs versus spaces is truly a matter of preference, however the tab is still the character specifically designed for indentation, and using one tab character per indentation level instead of 2 or 4 spaces will use less disk space / memory / compiler resources and the like.

Why are there 4 spaces instead of tabs?

If a couple of people work on same file it is highly possible to generate unnecessary conflicts. Using spaces instead of tabs makes it possible to easily catch such an accidental space on eyeball and this is probably a reason, why using them become a standard.


1 Answers

The problem is twofold. First of all, Haskell is indentation sensitive, e.g. the following code isn't valid:

example = (a, b)   where     a = "Hello"      b = "World" 

Both bindings need to be indented with the same number of spaces/tabs (see off-side rule). While it's obvious in this case, it's rather hidden in the following one, where I denote a space by · and a tab by »:

example = (a, b) ··where ····a = "Hello" »   b = "World" 

This will look like valid Haskell code if the editor will show tabs aligned to multiples by four. But it isn't. Haskell tabs are aligned by multiples of eight, so the code will be interpreted like this:

example = (a, b) ··where ····a = "Hello" »       b = "World" 

Second, if you use only tabs, you can end up with a layout that doesn't look right. For example, the following code looks correct if a tab gets displayed with six or more spaces (eight in this case):

example = (a, b) »       where»  a = "Hello" »       »       b = "World" 

But in another editor that uses 4 spaces it won't look right anymore:

example = (a, b) »   where»  a = "Hello" »   »   b = "World" 

It's still correct, though. However, someone who's used to spaces might reindent b' binding with spaces and end up with a parser error.

If you enforce a code convention throughout your code that makes sure that you only use tabs at the beginning of a line and use a newline after where, let or do you can avoid some of the problems (see 11). However, current releases of GHC warn about tabs by default, because they have been a source of many parser errors in the past, so you probably want to get rid of them too.

See also

  • A reddit thread on the topic (majority pro spaces, but some pro tabs)
  • Good Haskell Style (pro spaces)
  • Yet Another Tabs v Space debate (pro mixing)
like image 148
Zeta Avatar answered Oct 09 '22 06:10

Zeta