Wednesday, 9 July 2014

Tabs vs Spaces - Consistency is the key

We all know code indentation is a crucial process in any development codebase. It is always a conversion (or argument) that arises at least once a year within most teams.

Developers that know me, know that I'm a bit of a nazi when it comes to using 4 spaces - but I wont go into that too much. But there is nothing worse than opening a file to make a change and seeing an image like this:

So, all I want to put forward in this short but sweet post is

"You need a consistent standard!"

Whether it's 4 spaces or a TAB that equals 2 spaces, as long as your entire development team is on the same page you are in good hands - just don't let anyone break the standard!

This will ensure consistency across all of your current developers and new developers. Make sure you either document your standard for new starters or use a code sniffing tool so that errors are picked up on check-in before the problem can get out of hand.

Can't decide on a standard?

Your development manager should take input from the team and provide a standard for the team.
No development manager? You can always take the decision to the foosball table for a result :-)

Use an auto formatter

Code indentation can make code look beautiful - but it comes at a cost ('time' being that cost). I would hate to add up how many hours were wasted in my early development days manually formatting new and old code.

Every decent IDE these days has an auto-formatter for your code. It's as simple as configuring it consistently across your development team and code is automatically formatted each time a file is saved.

Why spaces?

Lastly, for those who really want to know why I push for spaces (and not tabs), here you go:
  • Spaces are consistent across different IDEs and operating systems.
  • Spaces don't inject special characters (like some IDEs when using Tabs)
  • Spaces are spaces - simple as that. Tabs can be interpreted as 4 spaces, 8 spaces (whatever the IDE is configured to be).
  • Because of this, spaces add less risk and worry about how a developer needs to configure their IDE before they begin coding.

Lastly, whatever your team ends up choosing, remember this...