A very specific argument that arises when discussing indentation styles is Tabs versus Spaces. For me, there is no argument. It’s the one thing I could never, ever, adapt to when writing code. Before I get flamed for my rant lets check the pros and cons and the typical arguments made by space-lovers. No, not the outer space, I mean the largest key on your keyboard.

View Code

If you use spaces, your indentation will always look the same, be it on your favorite IDE, vim, pico, joe, Kate, Geany, cat, less, more, github online, bitbucket online, etc. But… it will always look the same to you and everyone else. Let’s face it, we don’t all have the same tastes. When coding in group (job, school, hobby) we already have to agree on indentation style, so why should we also give in when it comes to indent spacing? If we had no option… but we do! We can use tabs and everyone can space it to whatever they want since almost any editor out there supports changing the tabsize. Even in your virtual terminal you can change tab size with  ‘tabs’ (if the VT supports it).

Editing Code

Previous argument in favor of using spaces makes me wonder if a programmer spends more time reading the code or editing it. It’s quite simple. Hitting tab costs you 1 key stroke. Hitting 2, 3, 4 or 8 spaces costs you… 2, 3, 4, 8 times more keystrokes. Look at your code. You will see that a great part of it shows you spent more time hitting the poor space bar (or tab key) than any other key.

Now is where you argue that in your editor when you hit ‘tab’ it fills in the spaces for you. Excellent! Now lets go back to the previous argument. You said it was so you could look at code anywhere and it always looks the same. Well… I prefer to edit code the same way on any editor. Tab is my universal indent key and delete/backspace is my universal unindent key. If you use spaces that is no longer true and you will end up making small indentation mistakes more often than not and that leads to very ugly code.

Bottom line is, spaces will always cost you a lot more keystrokes when compared to using tabs. The more time you spend typing white space the more time you waste because white space is non productive. Unlike using long variable/function names or writing lots of comments and documentation about your complex code.

The Script

If you are indeed forced to use spaces instead of tabs because whatever reason, you can always do like me and use a script to edit your code from/to spaces.

Save this to a file, put it in your path and then give it exec permissions (chmod 755 <file>). It takes 2 arguments. The first is either t2s (tabs to spaces) or s2t (spaces to tabs). The second one is if you only wish to change files which have a certain text. For example, I use:

This makes sure it only changes files which have me as the author (provided I put my name on all files I change). This script allows you to change all files, recursively, from the current folder where you are running the script from. So just set your terminal’s current directory to your source folder and run it.

Now lets look at that first line in the script since you probably should change somethings there.

These are 3 variables. First one is FILTER_FILES and it’s a regular expression to filter the files to process. The one I’m using filters C++ source (cpp) and header files (hpp). Second one is BACKUP_EXTENSION. I keep this in case something goes wrong. This one makes a backup of each changed file. The variable hold the extension used for the old file. You can then add this extension to your SCM ignore list. The SPACES variable is self explanatory, it means the amount of spaces that correspond to a Tab.