The indentation of lines

Today I have a question for you guys which bothers me every time I set up a new code editor. Recently it was Coda 2. As I always do when I get a new app I looked at all preferences, just to get an overview of the possibilities.

I stopped at ‚Tab Width: 4 – Use spaces instead of tabs‘, thought about it and didn’t check it. Until today I always used tabs to indent lines. I thought it was clearly the better way because it’s just one key stroke to delete an indentation. But since there is that option I wonder what the advantage is and how you handle indentation of lines?

Update: As it turns out in the comments, using spaces is the better way. A good editor (like Coda 2 or Espresso 2) will delete tab generated spaces with one key stroke. Going with spaces you also make your code future proof, backwards compatible and perfectly readable on every OS. Now I think this should be standard in every code editor. Do you hear me, Panic?

Veröffentlicht am Donnerstag, 31. Mai 2012

Kommentare

  1. For a few years, when I started to write code I used spaces. Thats was ok for me, cause I was the only one who worked on this code. But now I worked in a agency and in a team. So it’s better to use tabs instead of spaces. Someone like two spaces, the next one like 4 spaces and a other one like 8 spaces and so you have the problem that the code doesn’t look good and clear. So we all used tabs and everyone can choose his own option for the the tab-width.

    And when you write code for the publicity you should use tabs. So other people can easier see what you are doing there…the code looks clear.

  2. If you would use spaces instead of tabs your code will looke the same in every single editor out there. Some software manufactures allow you to choose the tab width – others not. If you’re the only one looking at your code choose whatever you want. Otherwise: Talk to each other and decide what you use.

  3. It’s mandatory to use spaces when working in a team. This is the only way to make sure that the code looks nice on every OS and in every editor. I would recommend to use spaces, even if you’re working alone. Do you know what’s going on in one year? Btw, a good editor allows you to delete indentations with just one key stroke, whether you use tabs or spaces. ;-)

  4. The first language I got to know where indentation actually matters was Python. Even if indenting with tabs or 4 spaces may look the same in your editor, it will still be two completely different levels of indentation for the interpreter – which will result in your code not working. The Python community has agreed on using 4 spaces for each level of indentation. This has always been my preference since then (well, html has so many levels of indentation that 2 spaces make more sense sometimes). It is also the way we agreed one in my current company. And yes, different editors might display tabs differently. Have you ever opened well formatted (with tabs) code in an old Windows Notepad? Urghs. :/ Every decent editor should include that option you discovered today. Using that will help you to use spaces instead of tabs. I have never missed the single keytroke „backspace“ to de-indent. Just indent with „tab“ and de-indent with „shift+tab“. Well, no matter what you decide on in the end – the only thing that really matters is not to mix tabs and spaces.

  5. The idea behind spaces is that you can’t set a custom tab width. A simple example explains it the best:

    Indentation with tabs (4 spaces wide): http://cl.ly/image/0o2R0V2s1G2R Indentation with tabs (2 spaces wide): http://cl.ly/image/2z0X0m2Q2z39 Indentation with spaces (4 spaces wide): http://cl.ly/image/410O1M322j1N

    While the indentation with tabs is messed up when the width is different, spaces stay spaces and the indentation looks the same everywhere.