Spaces or tabs to indent code?

Why would it be better to use spaces or tabs to indent code? I use tabs. Kirby use two spaces. A lot of discussion about it on the web, but what do YOU think?

Are you inviting bloodshed? :scream:

I’ve found that the majority uses spaces, partly because when you set your editors to “indentation = tab characters”, you quickly end up with a mix of spaces and tabs in the same files. When you set it to either 4 or 2 spaces, it’s a bit more manageable.

I use 4 spaces at work because that’s our team standard. I use tabs in my own code because I like the semantic aspect of “one level of indentation = one character”. And the two spaces style has gotten quite popular in the past few years.

The common recommendation is, for each project: pick one style, document it, and enforce it as well as you can. If you’re an agency working on many projects, having one style (instead of possibly one per project) can be good too.

(Edit: Kirby uses 2 spaces so maybe I should switch my plugin to 2 spaces. But I have a hard time reading PHP or JS with just 2 tabs, my eyes ain’t trained for such small indents.)

3 Likes

I generally use tabs as well because the width is flexible and can be adjusted by each developer.

But this is also a major issue you need to think about. Let’s take the following code:

$items = $page->children()
              ->visible()
              ->paginate(10);

Never use tabs for that or your indentation will break if someone else uses a different tab width than you. So I normally intentionally mix tabs and spaces in such lines, but that can be difficult to get right for people who aren’t used to it.

That’s why spaces are sometimes easier. For me it depends on the team I’m working in. We discuss it, document it and I put it in my Sublime Text project configuration. Done.

1 Like

For code files like *.php or *.yml I always use 2 spaces to indent.
But in text files (like word) I normaly use tabs, which I can ident with a flexible width, depending on the location of the tab.

Hint:
I use notpad++ on windows for code files :sweat_smile:
Here I can change all tabs to two spaces!
I can type the <tab>-key, which is configured to show 2 spaces or to delete 2 spaces, if I hit <shift>+<tab>.

2 Likes

I don’t care as long as I can configure my editor, then use tabs to type as many spaces as required :slight_smile:

(I’m actually used to read code with 2 spaces, so 4 spaces look a bit wide)

4 Likes

2 spaces :stuck_out_tongue:

Because my IDE’s do it at default (Rapid PHP and Visual Studio)… and all team-members (when collaborating online) also do…

When I coded html / css / js (and later on .asp / .php) “all by myself” (around 1995 or so) I used tabs -> but after years it ended up like a big mess (some IDE’s use 8 spaces for 1 tab, others use 4 spaces, etc… all the alignment made me mad!).

I also write a lot in C# / JAVA and all compilers take spaces as a default input… I don’t have any choise :stuck_out_tongue:

- edit - I do use the tab-key on my keyboards… but set the IDE’s up to interpret it as a double space!

1 Like