Thu 26 December 2019

Filed under Programming

Tags Style

There's often this huge debate about whether to use tabs or spaces in programming. I think there's only one real solution, but I didn't come up with it: Use both!

Image

Why tabs?

Tabs are easy and customizable. Need some indentation? Hit tab in any environment and you're there! Don't agree that tabs should have a width of 4? Well, change the settings of your editor to your hearts content.

What you shouldn't do with tabs, though, is alignment. Because tab sizes can be different, you shouldn't depend on it. If you do, things can look horrible.

Why spaces?

When using spaces, no matter the settings of the editor, the code will always look the same. But if you go for spaces, then make sure everybody working on the project is on the same page. Because multiple indentation sizes within one project is I think the literal definition of hell.

Why both, the correct way?

Want to do it correct in my opinion? Follow these two rules:

  1. Use tabs for indentation.
  2. Use spaces for alignment.

If you adhere to these two rules, then your code won't break when using different tab sizes. But you'll still have all the advantages of the tab.

There are many discussions about it online. This answer is very good and illustrates what I mean: Online Discussion.

What you should really do

While I think there's a clear best way to do it, the most important thing to do is adhering to existing standards. When I started with Rust, I was saddened to see that they went for spaces. But instead of just doing my own thing, I just accepted how it was and used spaces as well.

Now if I collaborate on code, I won't surprise and annoy the people I work with and if I do, it's their fault 😉

Comment

Carmine Crystal Blog © Dion Dokter Powered by Pelican and Twitter Bootstrap. Icons by Font Awesome and Font Awesome More