C coding style
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: C coding style
Oops, I missed all the discussion that's been going on since I last posted. It seems Solar had already covered the problem with using hard tabs.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re: C coding style
Tabs > Spaces.
Re: C coding style
*distributing pop corn*
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: C coding style
I think the whole tabs/spaces thing is largely irrelevant, as long as tabs are not used for anything other than indentation. When you use tabs to align things, like comments, the tab width can be important (as Love4Boobies pointed out.) However, when they are used for indents, the worst that can happen is that someone with larger tab widths will have some lines exceed the screen width. Also, if tabs are only used at the beginning of the line, using a regular expression to switch tabs with spaces and vice versa becomes trivial, so such decisions can be reversed. So... we shouldn't argue about it.
I prefer tabs in my own code, assuming no other precedent. However, since tabs cannot be typed directly into a web browser text box, using spaces for wiki code is the obvious choice for this situation.
I prefer tabs in my own code, assuming no other precedent. However, since tabs cannot be typed directly into a web browser text box, using spaces for wiki code is the obvious choice for this situation.
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: C coding style
Tabs for indents will cause misalignments of anything indented which is aligned
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re: C coding style
Write the code in a text editor, compile test it, and then paste it. Surely nobody sane writes code directly on the Wiki.berkus wrote:Please, how will you enter them into the wiki? I'm interested.Brynet-Inc wrote:Tabs > Spaces.
I use a combination of tabs and spaces, but it is common courtesy to adapt the style of the project you're currently working on.. OpenBSD's KNF works for me.
Re: C coding style
There, fixed that for you.NickJohnson wrote:I think the whole tabs/spaces thing is largely irrelevant, as long as tabs are not used for anything.
Re: C coding style
As far as tabs and spaces go, this makes the most sense to me.NickJohnson wrote:I think the whole tabs/spaces thing is largely irrelevant, as long as tabs are not used for anything other than indentation...
Re: C coding style
+1.Brynet-Inc wrote:Write the code in a text editor, compile test it, and then paste it. Surely nobody sane writes code directly on the Wiki.berkus wrote:Please, how will you enter them into the wiki? I'm interested.Brynet-Inc wrote:Tabs > Spaces.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
Re: C coding style
Sadly enough, not really.Darwin wrote:As far as tabs and spaces go, this makes the most sense to me.NickJohnson wrote:I think the whole tabs/spaces thing is largely irrelevant, as long as tabs are not used for anything other than indentation...
One, it is far too easy to "forget" that tabs should be used only for indents. Point in case, statements spread over multiple lines:
Code: Select all
// Assuming a TAB display width of 4
int main( int argc, char * argv[] )
{
>---// somewhat constructed, but you know what I mean
>---for ( int i = 0;
>--- i < n; // #1
>--- ++i ) // #2
>---{
>--->---// BODY
Two, not all tools support setting arbitrary TAB widths comfortably, if at all. Source isn't watched only in your favourite editor. You use 'less', 'diff', or whatever diff your VCS GUI of choice provides. More epecially, you cannot set TAB widths in the browser. You're advocating a 8-column indent? Really?
Three, we're talking about a Wiki style guide. Firing up a seperate text editor (and setting it to proper TAB handling if it's set to expand tabs by default) is a bit ridiculous. Alternatively, I could of course copy&paste existing tabs for proper indenting... You've got to be kidding me.
Do we really need to have a vote on this?
PS: Edited a wee bit. I got carried away at one point.
Last edited by Solar on Thu Jun 30, 2011 2:21 am, edited 1 time in total.
Every good solution is obvious once you've found it.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: C coding style
No we don't. Discussions of personal preference have already been excluded from changing the proposed style guide.Do we really need to have a vote on this?
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: C coding style
It seems I'll have to go pedantic on you Misalignment is not the only problem with tabs. Using them results in undefined behavior according to the C standard, as they are not guaranteed to be part of the source character set (unlike for the execution character set). There, your code is not only aesthetically volatile, but also invalid.
Except the style guide is more or less the preferences Solar and you had, just that no one really cared to impose their own.Combuster wrote:No we don't. Discussions of personal preference have already been excluded from changing the proposed style guide.Do we really need to have a vote on this?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: C coding style
Ahem, I gave my preferences.They just happened to be aligned with what Solar and Combuster were saying.Love4Boobies wrote:Except the style guide is more or less the preferences Solar and you had, just that no one really cared to impose their own.Combuster wrote:No we don't. Discussions of personal preference have already been excluded from changing the proposed style guide.Do we really need to have a vote on this?
Re: C coding style
As much as I would have loved that argument to be true , it isn't. (Yes, I immediately whipped up my copy of the C99 standard.)Love4Boobies wrote:Misalignment is not the only problem with tabs. Using them results in undefined behavior according to the C standard, as they are not guaranteed to be part of the source character set (unlike for the execution character set). There, your code is not only aesthetically volatile, but also invalid.
According to chapter 5.2.2 of the standard, the source character set includes "control characters representing horizontal tab, vertical tab, and form feed" (whatever good that would do).
Guilty as charged.Combuster wrote:Except the style guide is more or less the preferences Solar and you had, just that no one really cared to impose their own.
However (as Fanael rightly pointed out), a style guide is mostly about getting it over with... It's a big part about getting code that can be read with relative ease, and - in our special case of a Wiki full of tutorials and code examples for newcomers - another big part about teaching those newcomers some code style that won't make them the "ugly code style pariahs" in a pro development team.
I've worked on "trailing brackets" teams, and while I prefer the additional optical help of vertically-aligned brackets would've gone along with that. (It's a style mostly seen in the Java departments, and usually shunned by C/C++ departments, but there you are.) I've seen lots of "no padding spaces" code, and while disagreeing with that style as far as readability is concerned, would have gone along with that, too. Since I went into the role of "benign dictator" here, I naturally went with my preferences - not before checking that the issue at large still isn't "decided" in the web, and that I'm not pushing a fringe minority opinion.
But unfortunately the subject that's still discussed is the subject of TAB indents - which, in the context of the Wiki, has two clear cons IMHO (too-big indents and not being able to type a TAB in the browser editor) while offering no pros (as you cannot set TABs to your preference in the browser).
While I can somewhat see the benefit of compile-testing your code before posting, consider what happens if you want to convert an already-existing piece of code to TAB indenting: You'd have to copy the code into a different editor, convert there, then copy back... instead of fixing in-place. (Or using the copy&paste approach...) It's just highly impractical.
However, being pushy about a subject doesn't help. I've fielded my arguments against TABs, and will shut up now.
Last edited by Solar on Thu Jun 30, 2011 4:02 am, edited 1 time in total.
Every good solution is obvious once you've found it.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: C coding style
Oops, I got excited for nothing That sentence starts with "Both the basic source and basic execution character sets"Solar wrote:As much as I would have loved that argument to be true , it isn't. (Yes, I immediately whipped up my copy of the C99 standard.)Love4Boobies wrote:Misalignment is not the only problem with tabs. Using them results in undefined behavior according to the C standard, as they are not guaranteed to be part of the source character set (unlike for the execution character set). There, your code is not only aesthetically volatile, but also invalid.
According to chapter 5.2.2 of the standard, the source character set includes "control characters representing horizontal tab, vertical tab, and form feed" (whatever good that would do).
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]