Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Tabs or spaces for indentation?

Options
  • 12-01-2014 7:30pm
    #1
    Registered Users Posts: 25


    It's a pedantic question, but one that interests me, especially as I typically show whitespace characters when developing.

    In general I prefer tabs. With xml files especially it can make a huge difference in terms of file size. When you show whitespace characters it's also easier to read. Also I find that it makes editing a file easier, e.g. it's easier to delete one tab versus 4 spaces for example.

    The mains pro of using spaces is that they are consistent across systems and applications.

    More specifically with regard to xml files, but also others, such as java, what are the pros and cons of using tabs versus spaces for indentation, and for those that care, what is your preference?


«1

Comments

  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    Indentation... and Editors and brace positions. Whatever is consistent.

    I'm more curious about how big and how indented are these XML files that the file size becomes an issue? Are you sending them over the Internet? Do you compress them? Why do they need to be indented (other than for debugging purposes)? XML's not Python! :P


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Set your editor to replace tabs with X spaces - as dictated by whatever standard you use. All done.


  • Registered Users Posts: 25 FicheCuig


    I guess the OP is not clear enough.

    It's meant to be a general discussion on the pros and cons of tabbed versus spaced indentation.
    <a>
        <b/>
        <c/>
        <d/>
        <e/>
        <f/>
        <g/>
        <h/>
        <i/>
        <j/>
        <k/>
    </a>
    
    The above example is 69 bytes when indented with tabs, and 99 bytes when indented with spaces. The proportional difference increases as the file size increases.

    In answer to Colonel Panic's questions; first 3 questions: not limited to specific situation so not relevant. 4th question: human readability.

    Your example of Python is a good example where spaces should be used for indentation as tabs can have unwanted effects when being interpreted.


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    Sweet suffering carp. It's 2014 lads, we solved this problem in the 1980s when we wrote "indent". Pick a style of indentation you personally like, and rig your source control system triggers so that code is mangled into your personal style on checkout and into any generic style on checkin. Problem solved; use neurons for more interesting things.

    (and if the answer you're thinking of is "but my source control system doesn't have triggers", then you may be fixing the wrong problem...)


  • Registered Users Posts: 25 FicheCuig


    The OP must really not be clear! It's not asking how to indent; it's asking "what are the pros and cons of using tabs versus spaces for indentation".

    Clearly it's a fairly redundant topic and not one that interests most people (despite the replies); thus the inclusion of "for those that care" on the OP.

    Thanks anyway! :)


  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    I tab...which is set to write four spaces.
    :p


  • Registered Users Posts: 25 FicheCuig


    I tab...which is set to write four spaces.
    :p

    Best answer yet! :pac:


  • Technology & Internet Moderators Posts: 28,799 Mod ✭✭✭✭oscarBravo


    I hate tabs with a passion. I regularly review PHP code for someone on his servers, and whereas his editor is set to show tabs as four spaces, I often use "less" in SSH sessions to quickly page through files, and they immediately look like a mess.


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    FicheCuig wrote: »


    [/code]The above example is 69 bytes when indented with tabs, and 99 bytes when indented with spaces. The proportional difference increases as the file size increases.

    :rolleyes:


  • Registered Users Posts: 26,571 ✭✭✭✭Creamy Goodness


    what's disk space these days really? It's been a long time since I gave two ****s about it (about 10 years). It's only ever getting cheaper :D


  • Advertisement
  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    what's disk space these days really? It's been a long time since I gave two ****s about it (about 10 years). It's only ever getting cheaper :D

    Exactly its ridiclous to be proposing that a "pro" is a few bytes. Its a personal preference thing of which there is obviously no correct answer.


  • Registered Users Posts: 2,100 ✭✭✭ectoraige


    what's disk space these days really? It's been a long time since I gave two ****s about it (about 10 years). It's only ever getting cheaper :D

    It's not as cheap as bandwidth.


  • Registered Users Posts: 26,571 ✭✭✭✭Creamy Goodness


    definitely not, but again bandwidth is beyond the threshold of me caring about saving an extra couple of Megabytes of bandwidth. If you obsessed that much over every little megabyte you'd get nothing done and you'd have to be writing huge massive amounts of code for it to be noticeable.

    I get 150mbit/second at home (I think it's 500gb cap).
    I get 1000mbit/second at work (Unlimited).
    I get 40mbit/second on my mobile phone (Unlimited).

    All with data caps that are easily kept inside :pac:


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    FicheCuig wrote: »
    The above example is 69 bytes when indented with tabs, and 99 bytes when indented with spaces. The proportional difference increases as the file size increases.
    I missed this on the first read-through, but ROFL'd on the second :D
    Might I further propose that you don't use a whole colon where only a semi-colon is required? :D


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    definitely not, but again bandwidth is beyond the threshold of me caring about saving an extra couple of Megabytes of bandwidth. If you obsessed that much over every little megabyte you'd get nothing done and you'd have to be writing huge massive amounts of code for it to be noticeable.
    ...well. Unless you're writing code for mobile devices, when every little megabyte costs more than you'd expect over the air (data costs for GPRS/EDGE/3G/4G/etc are atrocious, and even if you have a good data plan, you still have to remember that the radio eats battery power, so using the network isn't free from that point of view either...).


  • Registered Users Posts: 26,571 ✭✭✭✭Creamy Goodness


    Sparks wrote: »
    ...well. Unless you're writing code for mobile devices, when every little megabyte costs more than you'd expect over the air (data costs for GPRS/EDGE/3G/4G/etc are atrocious, and even if you have a good data plan, you still have to remember that the radio eats battery power, so using the network isn't free from that point of view either...).
    Well yes that's definitely a case. Sending frivolous data down the wire will get you in trouble at some point if you're not careful, but when we're talking about indentation in source code then it's less of a concern.

    I'm a mobile dev/server backend guy, and I strive for data payloads to be as small and as concise as possible. Now if only clients were as strive-y :D

    I think we're getting a little away from the point though, spaces vs tabs is a argument that will never be solved just like iOS vs. Android (or whatever big two are next).


  • Registered Users Posts: 880 ✭✭✭moycullen14


    Neither. Complete waste of time. Run your source through the following and save huge amounts of disk space
    sed -e :a  -e 'N;s/[\n \t]*//g;ta'
    

    Also, dont ever use caps - they take up twice as much disk space (coz they're bigger)


  • Registered Users Posts: 25 FicheCuig


    ... again bandwidth is beyond the threshold of me caring about saving an extra couple of Megabytes of bandwidth. If you obsessed that much over every little megabyte you'd get nothing done and you'd have to be writing huge massive amounts of code for it to be noticeable.

    ...

    Well yes that's definitely a case. Sending frivolous data down the wire will get you in trouble at some point if you're not careful, but when we're talking about indentation in source code then it's less of a concern.

    I'm a mobile dev/server backend guy, and I strive for data payloads to be as small and as concise as possible. Now if only clients were as strive-y :D

    ...

    You first say that you don't care about "saving an extra couple of Megabytes of bandwidth" but follow it up with "I strive for data payloads to be as small and as concise as possible".

    For XML services, even with gzip compression for example, an XML file indented with tabs rather than spaces will be smaller in size. Worth thinking about if you "strive for data payloads to be as small" as possible (and other more efficient options such as JSON are not possible).

    I guess it was difficult for Chrome and Sparks (despite their reading of it several times) to see this benefit when looking at the simple example I gave! :pac: XML files can be much larger than that example, and can be sent at high frequency.


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    FicheCuig wrote: »
    I guess it was difficult for Chrome and Sparks (despite their reading of it several times) to see this benefit when looking at the simple example I gave! :pac: XML files can be much larger than that example, and can be sent at high frequency.

    Just ... no.
    If you're editing XML, you use indentation because you're a human and it makes things easier on you. If you're sending XML over the wire to another computer, why the hell are you putting indentation in there? Saving 30 bytes by using tabs? Just don't use indentation or whitespace at all, and save another ten...

    (Seriously, this is what the "structure" in "structured file format" is all about).


    And I think we need to be clear here if we're talking about editing a file or transmitting data when we start talking about what should have indentation and what shouldn't; and if we're editing a file that we save to disk, worrying about 30 bytes should be a long way down your list of worries, well behind "other developers being able to read my code without hassle". Seriously, if you really had storage limits so tight that 30 bytes made a difference, you shouldn't be storing human-readable source code on there in the first place. We invented cross-compilers for a darn good reason, after all.

    (well, actually there were many good reasons, but this was one of them).


  • Registered Users Posts: 26,571 ✭✭✭✭Creamy Goodness


    there's a huge difference between having indentation xml/json files and having xml/json transmitted down the wire.

    all whitespace that can be striped is stripped.


  • Advertisement
  • Registered Users Posts: 25 FicheCuig


    Sparks wrote: »
    Just ... no.
    If you're editing XML, you use indentation because you're a human and it makes things easier on you. If you're sending XML over the wire to another computer, why the hell are you putting indentation in there? Saving 30 bytes by using tabs? Just don't use indentation or whitespace at all, and save another ten...

    (Seriously, this is what the "structure" in "structured file format" is all about).


    And I think we need to be clear here if we're talking about editing a file or transmitting data when we start talking about what should have indentation and what shouldn't; and if we're editing a file that we save to disk, worrying about 30 bytes should be a long way down your list of worries, well behind "other developers being able to read my code without hassle". Seriously, if you really had storage limits so tight that 30 bytes made a difference, you shouldn't be storing human-readable source code on there in the first place. We invented cross-compilers for a darn good reason, after all.

    (well, actually there were many good reasons, but this was one of them).

    You might need to re-read again! :D That was a simple example, in real life there are much larger and more frequent use XML files, thus the savings will be greater than 30 bytes, and in data transfer more so than data storage, every little bit helps! :pac: There are also times when a client requires receiving human-readable XML rather than linearized XML, or it can be done just as a convenience for the cliet.


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    If data size is an issue then you shouldn't use xml! Parsing big xml files on a mobile device is brutal for example.


  • Registered Users Posts: 25 FicheCuig


    srsly78 wrote: »
    If data size is an issue then you shouldn't use xml! Parsing big xml files on a mobile device is brutal for example.

    JSON is (often) preferable for example, but you might not have a choice or you may still need to provide XML for some clients.


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    FicheCuig wrote: »
    You might need to re-read again!
    Someone definitely has to :)
    That was a simple example, in real life there are much larger and more frequent use XML files, thus the savings will be greater than 30 bytes, and in data transfer more so than data storage, every little bit helps!
    Indeed, so strip out all the indentation and whitespace before it hits the wire.
    There are also times when a client requires receiving human-readable XML rather than linearized XML
    Name three.

    Then name three that cannot be done by stripping out the whitespace on one side, sending the stripped XML over the wire or over the air, then on the far side adding whitespace back in the way indent has done since 1976 before passing on the now-indented-and-whitespaced XML to whatever client you were working with.
    or it can be done just as a convenience for the cliet.
    Then they can pay for that convenience, but that's not the use case we were talking about...


  • Banned (with Prison Access) Posts: 1,279 ✭✭✭kidneyfan


    TABS more consistent and just better!


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    Seriously. 1976, we solved this problem (though we've made other solutions since indent, like astyle and uncrustify, they weren't new, they were just doing what indent did, but to newer languages). Why are we still wasting cycles on this?


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    kidneyfan wrote: »
    TABS more consistent and just better!

    Did you miss the bit about tabs not being treated consistently?


  • Registered Users Posts: 25 FicheCuig


    Sparks wrote: »
    Someone definitely has to :)


    Indeed, so strip out all the indentation and whitespace before it hits the wire.
    Name three.

    Then name three that cannot be done by stripping out the whitespace on one side, sending the stripped XML over the wire or over the air, then on the far side adding whitespace back in the way indent has done since 1976 before passing on the now-indented-and-whitespaced XML to whatever client you were working with.


    Then they can pay for that convenience, but that's not the use case we were talking about...

    Multiquote! :eek: Worse than spaced indentation! You can talk about your use case, I am talking about the pro's and con's of tabs v. spaces for indentation. Since you think it's a redundant topic then there's noone forcing you engage in the topic.
    Sparks wrote: »
    Seriously. 1976, we solved this problem (though we've made other solutions since indent, like astyle and uncrustify, they weren't new, they were just doing what indent did, but to newer languages). Why are we still wasting cycles on this?

    Indeed, why are you? You clearly do have the time to waste since you have gone on so long about a subject which you consider a waste of cycles! :pac:


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    FicheCuig wrote: »
    you think it's a redundant topic
    No, I think it's a useless topic, it's like vi-v-emacs (in fact, it's actually well-known as being one of those holy war things). The answer is "do whatever makes you happy, and use tools for interoperability", and has been since the 70s...
    You clearly do have the time to waste since you have gone on so long about a subject which you consider a waste of cycles! :pac:
    303!


  • Advertisement
  • Registered Users Posts: 25 FicheCuig


    Sparks wrote: »
    No, I think it's a useless topic, it's like vi-v-emacs (in fact, it's actually well-known as being one of those holy war things). The answer is "do whatever makes you happy, and use tools for interoperability", and has been since the 70s...


    303!

    Redundant and useless are synonyms! Want to start a holy war on that!? :pac: I bet you could! :)


Advertisement