This article in 50 words: I used to prefer spaces vs tabs, now I don’t care so much, think it’s more important that you can easily switch on a per-project basis. Have some thoughts on how conventions should be established, and I’ll demonstrate bash code that can convert your codebase to a new standard.
Back in the day
I used to prefer spaces over tabs so code would look consistent throughout the monospace universe. No matter what crappy viewer people ended up using on it: as long as it supported a monospace font, your code looked as intended. I also did some work for PEAR and they enforced spaces in their thorough Coding Standards. Which was later adopted by many frameworks, sometimes with small deviations.
Quest for the holy Convention
After years of spaces in my code I started using CakePHP and their standard was tabs. Nothing to get hung up over, but after a while my code started intermingling with other Cake developer’s code and that’s when it gets a little hairy.
So I started using tabs there cause in my view conventions are much like traffic rules as I mentioned before in SQL Formatting
It’s irrelevant if people drive on the right or left side of the road, as long as they do the same
Adopt many Conventions
Coding standards change. Within a project, organization, framework, and even language. Or they change for you simply because you contribute to different aforementioned forms.
Instead of trying to enforce one preference throughout all of my projects, I adopt the rules of the domain at hand. In this order:
(where conflicting, left wins from right)
On a side-note, I think it’s the convention-designer’s responsibility to align his conventions with the layer they’re building on as much as possible. So frameworks should look at their language. companies should look at their framework. This makes for consistently looking codebases. And that helps encouraging involvement. Nobody likes messing with code that suffers from poor housekeeping.
In order to be flexible about this, it helps a lot if your IDE supports per-project settings (I currently use both NetBeans & Vim, and they do an fine job at that). In NetBeans it’s easy to mess up though cause it’s pretty much indentation agnostic. So sometimes you won’t notice you’re filling a 4-spaces file with tabs, ruining the code in other views/editors.
Once that happens, or maybe if you’re porting big chunks of ‘legacy’ code to a new standard that’s closer to your layer, you’ll need decent conversion scripts.
There are many pages on converting spaces to tabs on Linux or Mac, but I wasn’t satisfied as they:
- Also change non-leading whitespace (which may not be what you want, e.g. a tab-indented document could still use spaces to promote readability around assignments, or inside big strings)
- Don’t support multiple levels of indention
- Can’t be run from command-line (e.g. depend on IDE)
- Are specific to a language (
- Messed up my indentation (
In an attempt to come up with a reliable tabs vs spaces converter that you can simply run inside a directory and will traverse your source files, I’d like to share a couple of lines of BASH.
- Only do this when your source is under version control, these snippets make no backups!
So execute, test, verify, commit. Or hit
git reset --hardif you don’t like it (leave a comment for improvement!)
- Currently processes
.sh. But can easily be modified to do other extensions as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
On a Mac? You need GNU sed! - Read below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Run into problems?
Please let me know, I’ll update the article so that these lines become the perfect converters.
On a Mac? You need gnu-sed
Mac OSX (BSD) has a cripled
This illustrates my point:
1 2 3 4 5 6 7
Luckily you can get GNU sed for Mac OSX just as well. Get homebrew, then run:
And change all of
sed references to
How do you deal with changing coding standards?
These were imported from my old blog. Please use disqus below for new comments