Wednesday, 7 December 2016

4 space indent vs 2 space indent

Some developers like 4 spaces for the indent and some like 2 spaces. Originally I preferred 2 spaces, but ended up working for a company that insisted on 4 spaces, which is understandable to keep code consistent. And after using a 4 space indent for 5 years I actually prefer it for the following reasons:
  1. I find it makes the code more readable.
  2. Highlights when there is a lot of nested code, that might or should be refactored.
  3. It is consistent with C#, I know you can also change the number of spaces for tabs in C#, but 2 spaces in C# looks wrong to me.
I think this also raises the question on whether to use tabs or spaces, personally I don't mind and the fact that in Delphi you can set the number of spaces for tabs seems to get around the argument that people have that tabs take 1 key press. In reality I think that if a developer is actually pressing the space bar twice to indent or even 4 times, they are making life hard for themselves and should think about pressing 'CTRL' + 'D' to automatically format the code. I cannot see any argument for not using the auto format as long as all the developers on the team have the same configuration, else it does become a bit of a nightmare with source control.


  1. it was always 2, and should be 2 (IMHO). for other languages (C#, Python, SQL, etc) their standards should apply. don't try to fit your identation for all languages. be flexible.

  2. The default now for C# and MS SQL Server is 4 spaces, but is still 2 spaces for the Delphi source (System.Classes.pas). You say to be flexible and I agree that would be nice, however consider how you would write this in the coding standards for a company when you need to be consistent. Maybe the answer is to follow what the default is for the source, this would then also end the argument of where the 'begin' should be, one the same line (Egyptian style) or on a new line (as the source code).

  3. >don't try to fit your identation for all
    >languages. be flexible.

    The problem is that today most people use more than one language. Just as all IDEs (except Delphi's) allow you to change all of the key bindings so that you can have a consistent set of keyboard commands across IDEs and applications, it can be useful to implement a consistent style guide across languages one uses (so long as it isn't radically at odds with established norms for that language).