If there is something that I have never fully understood during the last years of learning programming languages, it's the chaos around indentation. Some says that you should use spaces, some others says that you should use tabs. And even if you're willing to use spaces, there will be multiple schools, some people saying that you should use two spaces, instead of four for some other developers.
Why is it such a mess ? I don't even see why it is such a debatable topic : indentation is made by tabs. Alignment is made by spaces. Nothing more, nothing less. Want to know why ?
Spaces are usefull because they have a fixed length. It means that it should be used when you have to align code. What's aligning ? Take this piece of code :
if ( a
....&& b
....&& c )
The dots should be spaces, because having a different alignment length would mess the code. If, for example, you have 8-characters-long tabs, you will end up with something like this :
if ( a
........&& b
........&& c )
So, in this case, you should definitely not use tabs.
Contrary to spaces, tabulations does not have a fixed size. There is no charset standard saying that tabs are 4-characters-wide : it's up to the interface to choose a suitable length.
Why doesn't they have size information ? It's because they are merely a semantic marker allowing to visually group some lines together. And what is indentation ? It's a semantic marker allowing to visually groupe some lines of code together.
Do you see the relation between the two of them ? And did you notice that both of them are defined as being 'semantical markers' ? It is the important part of the thing. If a developer wants to have a X-characters-wide indentation, then it should be possible, even if his coworkers prefer Y-character-long indentations. That's not a code convention, that's an editor configuration parameter. It's like if you were saying *"ok men, now we will use the same colors for our highlighters". It really doesn't make sense, it is ?
Look at this code :
if ( /**/ ) {
-> if ( /**/ ) {
-> -> if ( /**/ ) {
-> -> }
-> }
}
Does having a 4-characters-wide tabulation will change anything to the code ? No. Does having a 8-characters-wide will change anything to the code ? Well, it will probably have longer line but ... hey, if that's 8-characters-wide, maybe it's because the developer which is reading the code find it much easier to read this way ? Why would anyone would like to prevent them for using their own standards ?
Now we will talk about the supposedly greatest evil of our times. The mixed tabs/spaces.
They are far from being evil : they are the Right way. Remember what we said about alignment and indentation ? Well, it appears these two concepts does not overlap : alignment will never be indentation, and indentation will never be alignment. It also means that the two of them can be mixed. Look at the following code :
if ( /**/ ) {
-> if ( a
-> ....&& b
-> ....&& c ) {
-> -> if( /**/ ) {
-> -> -> /**/
-> -> }
-> }
}
With this code, the reader will be able to use his own tabulation size, but will not break the alignment.
Why should you use this kind of mixed indentation ?
- A source file will not be garbage if read in an editor with a different tab size than the writers
- The reader will be able to set its own tab size to indent the file, it will easing the reading
- Because it's the right thing to do. I mean, there is endless trolls about Linux vs Windows vs Apple, or Android vs iOS, or Emacs vs Vi(m), ... but the Spaces vs Tabs shouldn't be a troll : they are mean to be used together, not to fight each other.
- We are not in 1990 anymore. Nowaday text editors are able to plugin such indentation convention painless (Emacs, Vim, probably others).
These reason are even more important for library writers, because there will be potentially a bunch of readers. It's important to give them the flexibility of tabs, in order to lower code comprehension time.