I got dragged into another philosophical argument the other day regarding white space in code. It’s one of those quintessential tech discussions that does nothing more than rile people up over something that, for the most part, doesn’t achieve anything of value.
Generally speaking, I try and stay out of these discussions - however given I was trolling the person in this situation, I feel entitled to claim at least some small victory - someone has to.
That being said, I do have an opinion - and in the interest of adding to the pile of spew on this site that feeds back into the greater slum that is the internet - let me tell you what I think.
Let’s start with my personal preference:
- DO use tabs at the beginning of a line to indent code
- DO NOT use tabs after the first non-tab character in any line
- DO try and avoid indentation/formatting after code has begun
- DO NOT insert editor-aware comments in your source to set editor options
Easy! You’re well on your way to coding nirvana. I think this scheme works particularly well because:
- It doesn’t matter what size indentation you prefer - No more of this “I like my indentation to be 2 characters, not 4 or 8”. Please stop. That’s a presentation issue. Use an editor that supports configurable indentation widths.
- It’s semantic - The number of tabs corresponds exactly to the number of levels of indentation. You don’t have to worry about whether the author prefers their tabs at 2,4,8 or 19 characters wide - nor whether they prefer milk of cream in their tea.
- It helps filesize - Okay this is a bit of a weak argument, but using tabs reduces file size by not representing redundant information. Put your solution into a version control system and this has a slight multiplier effect.
And to cap it off, here are some common arguments as to why we should avoid TABs, and why I think you’re wrong.
- Not all editors support TAB characters properly - This has nothing to do with the file you’re editing. If the specification(grammar) for your programming language treats both space and tab as whitespace, then it’s whitespace. Your editor is not supporting the language you’re working with, and that’s your problem.
- I prefer my indentation at x characters - I’ve already addressed this above. Learn to configure your editor properly if it will let you, or find a new one that will. Vim, a terminal text editor from 1991 supports just about every possible combination of tabs and spaces, so I’m confident this is quite a lot easier than you might make out.
- My Wyse TTY is only 80 characters wide! - Brilliant! I also spend the majority of my time in 25x80 or other weird-sized, resizable terminals. Wouldn’t it be terrible to have to re-format the file every time your terminal changed size? That’s your editors job, and again it can reflow the text and preserve indentation if you’re using a good one.
Ultimately though, arguments like this are a zero-sum game - nobody is going to be 100% happy, and there’s always going to be that guy who has invested their sense of identity in their position on tab-vs-space - but you’re wasting your time dealing with them anyway, right?
Pick one that makes the leasy number of people cry, stick to it and move on - we’ve got interesting problems to solve.