Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » To tab or not to tab?

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
To tab or not to tab?
Michael Jensen
Member #2,870
October 2002
avatar

Quote:

Thanks to the magic of vim i can use tabs or spaces and have the behaviour and appearance be exactly the same except for using the arrow keys (but vim users use better keys )

are you telling me that when you press the backspace key, it deletes four spaces for you? and seriously, wtf.

Quote:

But i bet you don't use a fixed-width font. Try a different editor and you will understand

tabs look fine in fixed with, or non-fixed with fonts. spaces on the other hand will almost never line up with a non-fixed with font (except from the beginning of the line).

I copied your code into notepad, properly tabbed it, and copied it directly into a.ccs textbox, no problems, and into word with no problems, it works everywhere, I can easily navigate with cursors / backspace, everywhere. -- Anybody can copy and paste this into just about any sane editor, and bam it looks fine.

if (we_want_to_do_stuff)
{  // stuff inside the if is tabbed
  variables = long_html_function(
    stuff.sub_stuff->mother,  // immediately after first arg.
    the_next_argument,    // Comments positioned with spaces
    other junk,      // in line with other comments.
    0);        // the end
}

of course if you take the SPACES only version and paste it into your favorite document editor for printing AND bam:
.
http://www.allegro.cc/files/attachment/595265

SiegeLord
Member #7,827
October 2006
avatar

Quote:

Anybody can copy and paste this into just about any sane editor, and bam it looks fine.

Hehe, here's what it looks like in my editor (converted tabs to spaces to match appearance in my editor):

if (we_want_to_do_stuff)
{   // stuff inside the if is tabbed
    variables = long_html_function(
        stuff.sub_stuff->mother,    // immediately after first arg.
        the_next_argument,      // Comments positioned with spaces
        other junk,         // in line with other comments.
        0);             // the end
}

Make a guess why? ;) Doesn't change my point though. Better a few messed up comments than a messed up entirety of the source file.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Michael Jensen
Member #2,870
October 2002
avatar

shit you're right -- it's the right justification that matters again -- screwed up with spacing, and screwed up with tabs to different sizes

oh well, moral of the story is, use the same damn tab size! :-P

oh, and bonus points: it takes like 3 seconds to fix if you're using tabs... spaces? much longer.

Trezker
Member #1,739
December 2001
avatar

I'd say the problem lies in putting comments behind the code. That always gets messed up every time you change one of those lines of code, doesn't matter if you use spaces or tabs.

When you need to comment on a whole slab of code, write a slab of comments above the whole slab of code. Like a sort of pseudocode.

if (we_want_to_do_stuff)
{   
    /*! stuff inside the if is tabbed
        arg1 explanation
        arg2 explanation
        arg3 explanation
        arg4 explanation
    */
    variables = long_html_function(stuff.sub_stuff->mother, the_next_argument, "other junk", 0);
}

Karadoc ~~
Member #2,749
September 2002
avatar

Quote:

tabs look fine in fixed with, or non-fixed with fonts. spaces on the other hand will almost never line up with a non-fixed with font (except from the beginning of the line).

That's true. Tabs are better if you intend to code with non-fixed width fonts. But non-fixed with fonts have other issues anyway, so I don't like to use them.
For example, you can't do this:

/*****************************************************
** Here is a big important marker in my code.       **
** I'm surrounding it by stars to make it stand out **
*****************************************************/

So I don't use non-fixed with fonts, and I don't use tabs.

As for the tab version looking alright in "just about any sane editor", I agree that it looks alright, but the fact is that it still isn't formatted the way I actually wanted it to be. The stuff is nicely intended, but it is intended to some arbitrary line. I wanted it to be intended to the start of the open bracket, and I wanted the comments to start at the end of the last argument. Tabs do have some advantages for non-fixed with fonts and stuff like that, but I'm prefer the advantage of being have to format my code the way I like it (which can only be done with fixed with spaces).

I find it interesting that there are so many tab advocates here. I've just asked a couple of people about it at work and they all said they used spaces. The people I asked don't work on programming stuff together, so that is their own personal choice.

-----------

alethiophile
Member #9,349
December 2007
avatar

I like tabs when I'm using an editor that uses that, but normally when I code I'm using either Emacs or Gobby, both of which just insert some number of spaces when you press <tab>. So I'm fine with either.

--
Do not meddle in the affairs of dragons, for you are crunchy and taste good with ketchup.
C++: An octopus made by nailing extra legs onto a dog.
I am the Lightning-Struck Penguin of Doom.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I can't stand tabs. I use Code::Blocks and whenever I press the tab key it automatically converts a tab press into enough spaces to line up with the next tab alignment column. I use 2 spaces per indentation level and it just annoys me when I have to look at source code that uses tabs since they almost always mix it with spaces so I select and copy a tab , paste it into the replace all dialog and replace it with however many spaces I'm using. I hate tabs so much I think about writing up a patch to contribute for allegro's source code that nukes all the tabs right out of it. ::) If Code::Blocks would display all tabs as equal amounts of spaces maybe I could stand it but then there are problems when people mix tabs and spaces and use different indentation levels than I do.

Code::Blocks is especially nice for working with tabs since you can make backspace behave as if it was deleting a tab when there are spaces there and you can select blocks of text and tab them forward an indentation level or shift-tab them back an indentation level (handy when moving class source code from the header where I write it to the source file where it ends up and also when rearranging loops ;)).

CGamesPlay
Member #2,559
July 2002
avatar

Seriously. None of you are allowed to commit to my code base. You always use tabs, you never wrap long lines, and you always put comments directly above the code they document (with no fancy crap like surrounding stars). You do not line anything up with spaces, ever.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

The problem is that a lot of editors don't visually show you that you've typed a space or a tab so when people get forgetful and use spaces instead of tabs they aren't visually apparent so when that code gets viewed in a decent editor it's instantly apparent they've been mixing things and if you have a different tab size everything is screwed up.

With Code::Blocks if I read source code that has been consistently indented with only spaces , all I have to do is change my tab size and I can jump right in to editing the code without reformatting it. Of course if it has been consistently indented with only real tabs then I can just change the setting to not display tabs so I don't have to look at all the stupid arrows. Also that way each person could change their settings for the displayed tab size to their liking. The problem is like I said earlier - when people use tabs , they generally forget to be consistent because many editors don't give you visual cues and end up mixing spaces in. It's a matter of consistency is all and if I was working on a project with other people we would agree beforehand how everything should be formatted.

Peter Wang
Member #23
April 2000

Quote:

you never wrap long lines,

Yeah, great, then when you need to work on a different screen or you just want to have two windows side-by-side, you need to start scrolling horizontally. You can keep your write access.

Albin Engström
Member #8,110
December 2006
avatar

I hate space.. tab is the shit.

Actually I'm kind of a maniac when It comes to space.. as soon as i find an auto generated space in my dev-c++ project I tend to look thought the whole file and make sure there are no more spaces where there should be tabs. ;D

This is why i love notepad++ and hate dev-c++.

Onewing
Member #6,152
August 2005
avatar

Indentation is for making easy, readable and organized code. It's purely visual aesthetics. It seems the only problem with tabs is going from one editor to the next, which, as far as I'm concerned, shouldn't happen enough to outweigh my desire to use tabs.

I don't even know what the argument is.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Carrus85
Member #2,633
August 2002
avatar

Personally, since it is a pain to intermix the two, I'd rather use spaces for everything and tell the developers to use the tab <-> space conversion on any decent editor to indent quickly. This has the advantage of ensuring the code layout appears the same on all fixed-width layouts, and has the additional bonus of being able to use the tab key to create columns (since they insert spaces and don't look all screwed up on different tab stop widths). This also allows you to set a meaningful maximum line length as well.

Of course, this means that you need to have a code standard so that the automatic tab insertion works off of whatever indentation scheme (2, 4, 8, 2 then 4, etc.) you're working with. But you should have a coding standard anyway, so hopefully this isn't an issue.

And just as a point of discussion, using tabs or spaces may not be that important in most cases, but if your language is whitespace significant (*cough* python cough), it can result in programs that will mysteriously fail to even run.

The point is, consistency is key; if you're going to use tabs for indentation, always use tabs for indentation (and ensure everyone who ever touches your code base does the same thing). Same argument for spaces. Mixing tabs/spaces, however, is just asking for pain. (Unless you're using tabs for the left indent, and spaces to create columns, which works; have fun hitting the space bar 80 million times though).

OICW
Member #4,069
November 2003
avatar

I would be for tab indentation since it's crystal clear and you can set a tab size to whatever size you want. However I think that I've set-up my editors to use two spaces instead of a tab character, because I was unable to track down tab size. But hey, at least I'm using one style of indentation.

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

ixilom
Member #7,167
April 2006
avatar

I stopped reading the tabs vs spaces replies after someone said something about sucking it up. I agree.

Just use whatever the majority uses and stop bitching. Unless its like, "We like to torture bunnies in the toilet during lunch".

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

SiegeLord
Member #7,827
October 2006
avatar

Quote:

Seriously. None of you are allowed to commit to my code base. You always use tabs, you never wrap long lines, and you always put comments directly above the code they document (with no fancy crap like surrounding stars). You do not line anything up with spaces, ever.

I'm up for a coding style change soon, and I am definitely switching to that. An added advantage of this, is that most of the newer editors will allow you to fold comments that are located on separate lines, which helps clear up the code especially if the comment-to-code ratio is above 5... Unfortunately it only works consistently with C style comments, but that is for another topic.

Quote:

Yeah, great, then when you need to work on a different screen or you just want to have two windows side-by-side, you need to start scrolling horizontally. You can keep your write access.

Turn on wordwrap. ;) Manually breaking lines disadvantages people with 1600 pixel wide monitors. Or avoid using long lines altogether, those are generally indicative of something else being wrong: too nested a function, too verbose a function name, too large a tab size etc.

Quote:

This has the advantage of ensuring the code layout appears the same on all fixed-width layouts.

Yes, it looks the same on all fixed-width layouts. I.e. it (potentially) looks equally bad on all fixed width layouts.

In the end, I'd suggest picking a single, simple and consistent style for yourself/your group and sticking to it. If you need to interact with other people's code, do it via some automatic styling program/plugin like astyle. It isn't perfect, but if the styles are not too esoteric, this method will work best imho. I'm using tabs for my own use until someone with a gun comes and forces me change from that, but I understand other people's sensibilities too... so I replace my tabs with 4 spaces whenever I post my code on a.cc as a compromise.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Turn on wordwrap. ;)

Yep. And with the trend of all monitors going widescreen these days, you're just wasting space. KWrite does a sweet job of wrapping lines:

{"name":"595268","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/3\/734fc7b080d49354dbafa6e0d96be0ac.png","w":873,"h":435,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/3\/734fc7b080d49354dbafa6e0d96be0ac"}595268

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Matthew Leverton
Supreme Loser
January 1999
avatar

Wordwrap is evil.

count
Member #5,401
January 2005

Quote:

One of the problems with tabs is that a tab character needs to be converted into whitespace by the editor when displaying the file. How much whitespace should each tab character be replaced with? In an ideal world, the old typewriter functionality could be used, where each tabstop had a certain pixel position. That way people could even use proportional width fonts in their editors (instead of the blocky monospace fonts used by practically all code editors today), and the code would still indent nicely. However, no editor that I'm aware of supports this, so that's not a practical venue. Instead, editors typically make an assumption that a tab is either 8 characters (common in ye old days) or 4 characters (common in Java editors today). Some editors will stick with the 8 character assumption, but support 4-character indents in Java (which is common), so when indenting to level 3, they will insert a tab, followed by 4 characters, to get a 12 character indent using an 8-character tab.

Why is this bad? Because code is viewed in more than one tool. In the presence of tabs, code often gets misaligned. Code integration e-mail diffs, code viewed in other editors, code edited by other tools which treats tabs in a different way will easily get "mangled" (e.g. start getting mixed spaces and tabs).

link

Matthew Leverton
Supreme Loser
January 1999
avatar

I guess that settles it!

Vanneto
Member #8,643
May 2007

I agree, tabs win, spaces loose, close this puppy down. ;D

In capitalist America bank robs you.

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

link [blogs.sun.com]

As has been said, mixing spaces and tabs like that is stupid. ::) Any editor that does that is horribly broken.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

count
Member #5,401
January 2005

Quote:

Any editor that does that is horribly broken.

It doesn't happen because the editor is broken, but because more then one person edits one file.

As always. Having a convention is the way to go.
So if your boss says that it's done this way, then do it.
Conventions aren't always there to get the best result, but to get the same result from anyone.

We at work do have convention i don't like and I don't follow them in my private time.
But at work I do it because that results in similiar code from each developer.

OnlineCop
Member #7,919
October 2006
avatar

Wow; I read, like, 5 of the replies so far but there was too much blah-blah-blah for me to stay interested.

I like to use tabs to indent, but not to align, my code and comments. It irritates me when people use tabs as a fix-all kludge, because if I happen to like a tab to indent the equivalent of 3 spaces instead of 4 or 8, seeing someone else's code is all ugly and I either need to fix MY settings to see it "properly" or reformat THEIR code just so I can see things in the way they intended.

At work, they are 100% tabs (50% of the time, they use tabs instead of spaces between words as well, which irritates me even more). The only way I can live with it without going postal is the fact that I have several MASSIVE monitors that I don't need to scroll horizontally to see everything.

This is what I do at home, though (I'll show tabs with "--|" to indicate tab-spacing boundaries):

1#include <allegro.h>
2#include "myIncludes.h"
3 
4class something
5{
6--|public:
7--|--|something(); // Default constructor
8--|--|~something(); // Default destructor
9 
10--|--|unsigned int size(); // Return the size of the array
11--|--|char *getString(); // Return a pointer to the string
12--|--|void setString(char *); // Set the string
13 
14--|private:
15--|--|char *mString; // Private array for the strings
16--|--|unsigned int mSize; // Size of the string
17};
18 
19// ...and so on, and so forth...
20 
21int someFunction(int aaa, char *bbb, bool flag = false)
22{
23--|// First, we're going to do something with 'aaa',
24--|// then something else with "bbb", and finally, check
25--|// whether or not "flag" is set to TRUE. If it's not,
26--|// life as we know is must end and all people must die.
27 
28--|// ... stuff ...
29 
30--|int reallyLongVariable = someScope::ID_TRUST_ME; // Notice where the indentation
31--| // is on this line, and that I pad with a
32--| // single TAB, and then fill the rest with
33--| // spaces to ALIGN.
34 
35}

So as the example shows, no matter WHAT my tab settings may be, all the comments (especially those hanging comments near the bottom) will be aligned: your tab settings may be at 1 character, or 8, or 4,8,12,20,24,32 for all I care: it all lines up in the end.

So I like to indent with tabs, but after I followed the previous line's indentation (so if it has a couple tabs, I match those tabs), I use spaces to actually align everything up to look nice and orderly.

Sorry if someone already answered this above, and I just didn't feel like reading your posts/comments. :P

Evert
Member #794
November 2000
avatar

Quote:

Isn't this more of a there arn't any arguments for breathing either -- you just do it -- sorta thing? Or is there really a good reason not to use tabs?

To paraphrase, yes, this is that sort of thing: if they tell you to use spaces for their programming style, you use spaces. If they tell you to use tabs, you use tabs.

I find the whole argument increadibly silly. It's like C vs C++, Windows vs Linux, Vim vs emacs, Allegro vs SDL. You can bitch and argue and rationalise about all of those as much as you want, in the end of the day it's personal preference which you prefer. That said, if you work on a project with multiple contributors, you follow the coding style set for that project. It is much more important to follow a consistent style than to follow the style you're most used to.

As to my personal preferences, I used to use tabs but nowadays I use spaces. I think Peter's post gives a neat summary as to why. A lot of the programming I do on a day-to-day basis involves legacy FORTRAN code, which you're not officially allowed to indent with tabs to begin with (and lines start with six space indents). Some compilers let you anyway, but some don't.
As to the "pressing tab once is better than pressing space several times" I've seen come up again, get a real editor. You know, one that can be configured to insert spaces when you press tab and can treat multiple spaces as one tab.

 1   2   3 


Go to: