Tab instead of four spaces

Hi, just wanted to ask about the reason tab should be used instead of four spaces? Is it purely stylistic? Everywhere I’ve worked (C# web development) the convention was to use spaces (either 4 or 2) instead of tabs because tabs look and act inconsistently across software and operating systems. I’m kind of used to setting VS to add spaces when I press the tab button.

Anyway, it’s not an issue as I’m using the VS at home just for this, but I still would like to know any advantages to using tabs instead of spaces.

I just realized this was a bit long, and an Unreal topic, so you were probably talking about C++
I can’t say for sure in that regard, but there could possibly be compiler issues if you use one over the other. I’m pretty sure C++ is like C# in the “it’s preference” aspect of it, though.

I think the C# style guidelines suggest 4 spaces instead of straight tabs, but I could be wrong. I personally have always used the spaces, and most auto-formatters for C#, even the ones built into Microsoft’s C# extensions for VS Code will convert tab characters to 4 spaces. My IDEs and editors all are set up to use spaces instead of tabs, largely because different programs and OSes have their own version of what a tab is (Notepad default on Window is equivalent to 8 spaces) but a space is a space. I think there are some compiler issues that might come into play as well, depending on the language. I’m pretty sure C# is built to be forgiving of whitespace anyway though, so most compilers will probably just strip it all away anyway, so it’s almost certainly more for styling consistency than anything else, in this case.

The reason Ben instructs us to use tabs instead of spaces is probably just to conform to the Unreal Coding Standard:

This way, we can get used to that coding standard from the beginning.

Anyway, in theory, the good thing about tabs is that developers should be able to setup their IDE to consider the tab width as N spaces in the way they like. So, everyone could have their preferred visual indentation and be happy. In practice, however, even when using tabs in the beginning of lines, developers will sometimes still try to visually align code with spaces in some situations. After some copy+pasting around and using return to add more line breaks, things tend to end up with a mix of tabs and spaces without anyone realizing. This is terrible, because then the code will only look nice and tidy when the tab width is kept at a specific setting. Any other setting will result in a completely messed up visual indentation. This is usually why many other standards choose to just use spaces and avoid tabs altogether.

Privacy & Terms