Indent with two or four spaces

· Allanderek's blog


I've always indented Elm with four spaces. To my mind this looks nicer than two or eight spaces. It also seems to be about the default, relatively common in code seen in the wild. Recently I've found a really good reason to prefer two spaces, but I still cannot quite make myself accept it. I thought I would detail it here anyway.

The 'problem' with four spaces comes when you want to indent nested comma separated entities. So a list of lists, or a list of record types. Suppose you have a list of people:

 1people : List People
 2people =
 3    [ { name : "Ringo Starr"
 4      , yob : 1940
 5      }
 6    , { name : "John Lennon"
 7      , yob : 1940
 8      }
 9    , { name : "Paul McCartney"
10      , yob : 1942
11      }
12    , { name : "George Harrison"
13      , yob : 1943
14      }
15    ]

So the problem is that when you indent to continue the record expression which is the first element in the containing list expression, you naturally indent by 2 spaces, because then the , of the line , yob 1940 lines up with the with the opening brace of { name : "Ringo Starr". This means that you now have inconsistent indentation increases, where most of them are 'four' spaces but in the special case that you're continuuing a nested comma-separated entity you increase the indent by two spaces. This problem goes away if you just always indent by two spaces:

 1people : List People
 2people =
 3  [ { name : "Ringo Starr"
 4    , yob : 1940
 5    }
 6  , { name : "John Lennon"
 7    , yob : 1940
 8    }
 9  , { name : "Paul McCartney"
10    , yob : 1942
11    }
12  , { name : "George Harrison"
13    , yob : 1943
14    }
15  ]

My editor (nvim) has a nice increase/decrease indentation command (<< and >> in normal mode), it even nicely 'snaps' to a multiple of your chosen indent (so for me normally 4), so this situation kind of breaks my editor indent/unindent commands.

Hence, switching to just always using 2 spaces for indentation in Elm seems to be an easy win, but try as I might, I just cannot reconcile myself to using two spaces for indentation, it just doesn't feel enough. So for the time being I'm going to live with the inconsistency.