Another thread was about this, I was just wondering: how do you indent your source code?
I prefer indenting with two spaces.
Printable View
Another thread was about this, I was just wondering: how do you indent your source code?
I prefer indenting with two spaces.
4 spaces. I used to use tabs until I realized it made the source code look different depending on what application was viewing it.
2 spaces
@Echo bithub
I bet a bunch of newbies are going to read this thread and be like "huh? wuzzat???". :p
Anyway, one tab or 6 spaces.
So, we need to periodically hold this poll so as to determine some trend? :)
Well, there seems to be several threads about indent style, but the one that most closely matches this poll is... How far do you indent?
I use tabs (set to the width of 4 spaces) because we use Visual C++ exclusively for our coding and tabs are easiest to work with.
On personal projects I often set my editor to convert tabs to spaces (still at 4 spaces per tab). For code I post on forums I have a macro that converts all tabs to 4 spaces that I run before posting since tabs don't look so good on these forums.
I'd like to add that one space just isn't sufficient. On a side note, I wonder if anyone indents using only semicolens? :p
I follow Linus's lead:Quote:
Originally Posted by Linus Torvalds
When I'm indenting blocks of code like functions, conditionals, etc... I use tabs.
When I'm trying to get various parts of the code to line up (like when I have very similar lines of code and I want to emphasize their similiarity and clarify the different parts of each line, I use spaces (that way it doesn't get messed up if someone uses 4-space-tabs instead of my 8-space-tabs).
edit: Linus Torvalds endorses my 8-space-tabs? YES!
That just makes the three of you heathens :pQuote:
Originally Posted by sean
Who is Linus Torvalds?
EDIT: Just found it on Wikipedia. Didn't look too impressive. But 8 space tabs surely look impressive on C++ generic programming...
I use tabs at work. The reason is that some people like a larger visual indent and some people prefer a smaller one. By using tabs, you can adjust this by changing your editor's tab width.
For non-work, I indent with 4 spaces. I don't really have a strong feeling about it, but what I do hate are source files that have been mangled up with a mixture of tabs and spaces.
I like tabs and always keep my code 80 characters wide at most. Only one problem though: Let's say your company has a rule that code should not be more than 80 characters wide, and you start using tabs that are only 4 characters wide. What happens when someone who likes their tabs to be 8 characters wide opens your file and find that every other line soft wraps or disappears into the marginal?
So true. I can use either tabs or spaces but it absolutely sucks when someone uses both.
I personally use tabs as it makes doing find/replace regex a lot easier, plus hitting the tab key is easier than hitting the space bar a bunch of times. Though with a good text editor you don't have to worry so much about that as it takes care of it for ya.
>It would allow us to see how everyone slowly trends from their
>imperfect indentation style, to the correct style of 4 spaces over time
Heheh.
>I was just wondering: how do you indent your source code?
I prefer two spaces, but any even number is comfortable. Odd numbers are unnatural.
I use 8 spaces (set my editor to convert tabs to spaces). No particular reason.
This pool has become a revelation to me. I was pretty sure until now very few people used ASCII(9) for their code indenting. And I certainly haven't been seeing much of that when looking at code online or when copy pasting it to my editor from many different sources.
My guess... this pool is fixed!
tabs are evil.
I most definitely use ASCII(9), a.k.a. '\t'. It's the best way to go to keep things properly aligned, in my opinion. I hate it when I edit code that uses spaces and I have to backspace a million times to delete an indentation instead of simply backspacing once.
Depending on your editor and its configuration, you may only need to backspace once, or to use something like shift + tab.Quote:
Originally Posted by DavidP
Anyway, I note that this thread is about the amount by which one indents, not about whether one indents with space characters or tab characters.
With vi, I use:
:set tabstop=4
:set shiftwidth=4
:set expandtab
:set softtabstop=4
With these settings, you don't even know you are using spaces instead of tabs. The tab key, delete key, and formatting behave exactly as you would expect. Plus you get the added benefit of consistent formatting since you are actually using spaces instead of tabs.
There is probably equivalent settings in other editors, but why would anyone ever want to use a different editor? :)
>I note that this thread is about the amount by which one indents, not
>about whether one indents with space characters or tab characters.
The amount is far more interesting. But I use real spaces because the amount is small enough that there's really no difference between tabbing once and spacing twice. If I used an indent of four or more spaces, I would be more inclined to switch to tabs.
Me, I like tabs. Usually I think that 4 spaces is an ideal indentation level, and it is the default of Visual Studio, so that is what I use.
Aye, but I see that either I missed the ": spaces or tabs", or EVOEx has changed the topic.Quote:
Originally Posted by Prelude
I used to do that. . . then I started writing real code and it got sooooo ugly and unreadable that I just couldn't do it anymore. But, I use vim as my editor so (in the old days) I was stuck to two spaces = two spaces and not an indent.
Someone coding with two spaces should be taken out an horse whipped. ;) just kidding.
Then the company policy is incomplete. If you specify that tabs must be used, AND that lines are to be viewed at 80 columns, then you MUST specify the tab width or your specification is incomplete (obviously).
Here, since we do not specify specific tab width, we therefore leave line length unspecified as well.
>then I started writing real code and it got sooooo ugly
>and unreadable that I just couldn't do it anymore
Real code gets out of hand quickly, not because of stylistic choices (assuming those choices are reasonable), but because real code is complicated and always evolving. It takes more work to keep it clean and readable, regardless of how much you indent. ;)
True that. One of the other paragraphs in Linus's coding style document (in the Linux code) says that one must keep functions short and to one point. If a function is longer than a screen shot (25 lines), then the function is probably too complicated (except for the case in which the function does IO or has a nasty switch statement handling many cases).
I've tried my very best to adhere to what Linus wrote -- his was a morph of the K&R style -- which they didn't teach at JSU -- we were just kinda left to our own devices about the style of our code.
I'm pretty sure my editor uses tabs, so I use tabs, still, and always. In fact I expect my code indented whenever I type a brace and press enter. The length usually looks like 4 spaces but it's actually 8 spaces.
Is the fact that you can no longer see straight supposed to be some sort of justification here? I mean I sometimes like marathon sessions but I would not use them as an excuse ("after 5 hits of acid and a case of beer, you'll find it a lot easier to see how the indentation works if you have large indentations"). If you are becoming confused by 4 char tabs, please call it a night.
I use tabs set to 4 characters but I don't care much, 2, 4, or 8 is fine. I switched from 8 to 4 because a block nested 6 or 7 levels deep is so far inset that you must start scrolling horizontally for long lines. I didn't vote because the poll seems flawed to me, insofar as it contrasts "a tab" with any number of spaces.
I'll draw the line at one, tho. 1 character is not indentation.
Does no one use 3 spaces?
2 is too few because everything looks more flush than it should. My code tends to have deep nests, and I abhor the horizontal scroll bar, so anything more is overboard. Like, 8. Seriously?
I'll often use the tab key, but only if I've set the IDE to print 3 spaces instead.
I've found that this is usually due to someone ripping someone else's code, or, because the same file was edited in different editors.Quote:
...but what I do hate are source files that have been mangled up with a mixture of tabs and spaces.
Yes. I wasn't criticizing Kennedy, I was criticizing Mr. Torvalds. However, the part of the kernel source that I have looked at does seem to be mostly short functions and the nesting never gets past 3 or 4 levels, so maybe he is trying light the way down a certain path, I dunno.
Seriously, what do I use? I use everything. The whole point of styles is that you stay consistent with existing code.
Single tab for work. Four spaces for Boost. Two spaces for Clang. I've even used the evil GNU indentation style when working on GCC (I still have nightmares about that). What am I supposed to do with this little single-choice poll?
Tabs, with equivalent size of 3 spaces.
I don't code crazy stuff like:
...so using tabs is of no problem for me.Code:void Func(int X,
int Y,
int Z);
3 spaces, since I'm hardcore. And using non-even indent levels seems to have it's advantages (I'll get back to you when I work out what they are :)).
Plus, for vim I highlight all the trailing spaces and hard tabs so I know if some tool has mixed them up. Yes amazing... For those of you who want to unlock my magic
On a side note, who uses black backgrounds for their coding window? I find it's easier on my eyes.Code:set list listchars=tab:\|_,trail:.
EDIT: I'm disappointed Linus has popped up in this thread too! What is it with him?
I've used it pretty much all my life. Always around some sort of small variation of this style. Except back in the days when we had little choice and it was either white on blue, or green on black.
But recently I've been having problems with it. I'm getting tired faster and, I reckon, having more trouble concentrating with it. My ophthalmologist insists it's the fact I'm getting old and the quick variations between dark and bright white when switching between the editor and the web or other materials is not being taken easily anymore by my eyes.
I have been doing some forays into lighter backgrounds, while making sure I do not increase the contrast too much, testing some possibilities until I settle with something I like. Currently this is what I have, but still working on it. I'm not entirely satisfied yet.
OldBrownEyes Scheme:
The font is Dina. Scientifically proven by the major makers of software to be the best font to protect your code from corrosion and calcium oxide while leaving a good natural smell and protecting the environment.
Me too. I had thought this was scientifically proven, but after googling around it looks like the world is split on the issue and there is no evidence, beyond personal preference, to justify either dark on light or light on dark.
I'm also sold on large fonts; I used to use normal fonts and then just jack them up sometimes when my eyes got tired. Now I just leave them all large all the time and hey! my eyes never get tired.
ps. great vim tip, thanks! I use all tabs, but at least I can see them now. Apparently this has it's own highlight class:
Code::highlight ExtraWhitespace ctermbg=darkblue
man 9 style
I agree that white-on-black is nicer on the eyes, but for some reason, I find it more natural to read thinner fonts on black-on-white.
Mario, nice theme, you should try adding a little more green to it, the preprocessors would be a good place.
No one is extreme enough to use 1 space for indenting.
Which is odd since it's the optimal indent level for a small file size, while still having indenting :)
Using tab characters would result in the same small file size with indentation that is easier to spot. But "small file size" is a poor argument anyway.Quote:
Originally Posted by zacs7