Indenting your code – how do you standardise?

Posted on

I asked a question yesterday on Twitter about how people who work in large teams of developer (my example in this case was 10+) keep their code formatting/approach in check across that number of people.

Thanks to a retweet from the wonderful Rachel Shillcock (rachil.li), I got a whole bunch of replies, many of them suggesting that I should implement the use of a large stick and a certain amount of violence to make sure everyone keeps their code in order.

One of the more technical suggestions came from Martin Bean (martinbean.co.uk), and he said the PSR guide is a good one to follow.

So I took a quick look and my attention was immediately grabbed with one of the bullet points:

Code MUST use 4 spaces for indenting, not tabs

This put me right off!

I’ve always used tabs to indent my code, from the days when I was at school programming in RM Basic (yes, I am rather aged).

I personally have always found the use of spaces to be very restrictive. When I work with other developers on a project, some prefer the look of a 2 space indent, whereas sane people, like my good self, prefer 4 spaces.

The use of the tab allowed each developer to individually set their preference in their IDE to whatever display of the character they wanted.

As long as the indentation was created using tabs, the indentation itself would be standardised, but the developer would be able to control how the tab character is displayed to them.

Martin said:

I’ve always favoured spaces. Spaces are uniform, tabs aren’t. Code *should* be indented by four spaces

My counter-point was:

Surely tabs are more flexible than spaces in that case?

And then he said:

But standards are supposed to standardise things; everyone should be singing from the same hymn sheet

This was the statement that cleared it up for me.

Everyone has their own preferences, but individuals have to make sacrifices in the way they prefer to write code for the greater good.

This is particularly poignant for me as I’m in the middle of writing up some coding guidelines for the dev team at @thinkbda.

The changes I’m planning are  going to annoy some of the guys in my team, and I’m going to be one of those annoyed individuals. I will sacrifice some of my preferred methods when writing code to help standardise everything that comes out of our development studio.

So what sacrifices have you made to adhere to standards in a team of developers?

Have you had to adjust or create your own guidelines like I’m doing now?

Let me know in the comments below.

 

This entry was posted in Processes, Standards.

17 thoughts on “Indenting your code – how do you standardise?

  1. spacer Rachel says:

    I’m glad you got so many responses from the RT, Wes! spacer

    Personally, I always use tabs – I actually asked the very same question on Twitter a few months ago (“tabs or spaces?”) and the overwhelming answer was actually to use tabs!

    I’ve found that one thing I love and – to me – makes really legible CSS is to indent particular rules that relate to each other.

    For example, if you have a header and inside the header you have a nav (with a ul inside) and an image that you have given the class of .logo, the way I would lay this out would be like this: hastebin.com/hafulagije.css

    But again, that’s my preference – it isn’t for everybody! Hope you get some good guidelines sorted soon spacer

    Reply
    • spacer Westley Knight says:

      You asked the same question a few months ago?! I’m am massively behind the times!

      I agree with the legibility aspect, good indentation, however it may have been standardised, really helps.

      That hastebin example is how I love to do it, but it’s going to be one of those things I may have to sacrifice when it comes to writing up guidelines for my team.

      And thank you for the encouragement, I may need some more of that!

      Reply
  2. spacer Hughie Wilmshurst says:

    WHAAAAT??!?! Oh no you didn’t!

    In protest, I’ll use spaces, but I’ll set it to 13 spaces per “tab”.

    Reply
    • spacer Westley Knight says:

      I will consider your suggestion and put the choice forth in our next code review meeting…

      :o)

      Reply
      • spacer Hughie Wilmshurst says:

        Pfft :p Using tabs results in less characters, they’re flexible so if you prefer “2 spaces”, your editor will look like 2 spaces, and then when I open it up in my editor, it looks like 4 spaces, which I prefer. Using tabs as a standard would mean we can all work in an environment that looks familiar and comfortable, without the worry of opening someone elses file and going “oh what the hell is this shit”

        I’m voting for Tabs! In fact, you’re not in the office yet, I’m going to remove your spacebar. HOW DO YOU LIKE THEM APPLES?

        Reply
  3. spacer Alex Davies says:

    I’ve always indented with tabs, always always always, for the exact same reasons. Until my latest job that is, where all code is reviewed by peers before being signed off (mostly to check for stupid mistakes) and we used 4 spaces for indentation. Luckily editors such as Sublime make this switch very easy to do.

    I’ve started using 4 spaces as indentation on my personal projects, but only because that’s how I’ve got my editor setup for my work stuff. I’ve not really noticed any real differences, but if I was one of the people that enjoyed using 2 space indents (or the equivalent tab size) I would be very annoyed by the practice.

    Not to mention a tab = less characters in your code spacer

    Reply
    • spacer Westley Knight says:

      I really like the idea of peer reviews of code. It’s definitely something I want to look at incorporating in our workflow, and it means the developers can give each other a good doing over with the ‘bad developer sitck’.

      Love the idea of saving 3 characters per tab for file size!

      Reply
      • spacer Alex Davies says:

        We have to have peer reviews due to the nature of our code, if I make a mistake it could crash a server and cause issues for a lot of people (talking millions). With a web development agency, although they would be helpful for keeping standards up and cleaner code, it would definitely feel a little excessive at times.

        Reply
      • spacer Alex Davies says:

        Just to add, I did once get called up on using tab indents on about 40+ files once. I felt that was a little bit harsh spacer

        Reply
  4. spacer Alex Davies says:

    @Rachel

    That style of indenting CSS is great, I’ve seen it become a lot more common recently, especially with the rise for pre-processors such as LESS and SCSS, people you seem to use these languages a lot have started keeping the indentation style for standard CSS whenever they write it.

    Reply
  5. spacer Dan Blundell says:

    I always love this debate, everyone has a different opinion on how code should be formed. Personally I agree with Martin’s ‘standards are supposed to standardise things’ comment, whether or not you agree with the standard is irrelevant, it’s a standard.

    Whatever your ‘standard’ may be, as long as the people involved adhere to it I think that’s okay. I find outlining what you expect from the beginning is best, that way if someone chooses to do something else, it’s clear that they’ve gone against that standard.

    We recently started putting together a styleguide for markup, I haven’t gone so far as to say ‘code must be indented by 4 spaces’ just yet but what this does do is create a consistent approach – that’s probably the key more than anything, consistency.

    I use what I’d call common sense – CSS attributes in alphabetical order per selector, running JS through JSLint, just simple stuff that makes code easier to read!

    Reply
    • spacer Westley Knight says:

      I’m doing the same thing with CSS at the moment and writing up a style guide.

      I was of the same school of thought for the alphabetical approach, but after reading @snookca SMACSS, I’ve converted myself! Might be worth a look: smacss.com/

      Reply
  6. spacer Jaik Dean says:

    Consistency really is the only thing that matters. Put bluntly, anyone suggesting the character savings of tabs over spaces amount to anything other than some strange file size vanity doesn’t understand much about performance.

    Consistent code using spaces when you prefer tabs, or vice-versa is far, far nicer to work with than a mish-mash of styles.

    Reply
    • spacer Alex Davies says:

      I feel like that comment was slightly directed towards one I made earlier.

      I’m yet to see anyone seriously suggests tabs over spaces due to file size, it gets thrown into a lot of arguments by many (myself included) but quite obviously is meant as a joke.

      Consistency is definitely the only thing that matters, which is why code style guides exist. Having to readjust every time you switch file because someone decided to use spaces instead of tabs, 2 spaces instead of 4 or maybe even 3 spaces just to piss everyone in the building off gets annoying. We’ve all had to do it when working in teams, lightly suggested style guides are never adhered to.

      We haven’t even started discussing new lines for { (we actually use BOTH at work, that’s an interesting one to be consistent at), spaces after function variables or commenting style.

      Reply
  7. spacer Garry Aylott says:

    I’ve always used tabs to indent my code and in Coda 2 I set it up so that tabs indent by 2 spaces. Going by 4 spaces seems to look to spaced out for me.

    I guess it’s personel preference and most importantly, whatever you decide, as long as you’re all doing the same thing it’s all good.

    Reply
  8. spacer Rob says:

    Tabs or 4 spaces? Personally I’ve used both but tabs is my preferred. But surely, as you rightly point out a ‘standard’ isn’t just an industry standard but a work environment standard. Everyone needs to sing from the same hymn sheet in a studio, be it tabs, spaces and another habits and idiosyncrasies we all pick up along the way.

    Reply
  9. spacer Shane Hudson says:

    I have not yet standardised myself let alone anyone else… I have been writing code for over 10 years, and I still swap between tab and space (for spaces I always set tab to be spaces) and cannot decide. I get the pros and cons of each, and find it tends to depend on the language and/or the text editor.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a class="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>