C coding style

All about the OSDev Wiki. Discussions about the organization and general structure of articles and how to use the wiki. Request changes here if you don't know how to use the wiki.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: C coding style

Post by Love4Boobies »

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 ]
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: C coding style

Post by Brynet-Inc »

Tabs > Spaces.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Re: C coding style

Post by Neolander »

*distributing pop corn*
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: C coding style

Post by NickJohnson »

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.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: C coding style

Post by Owen »

Tabs for indents will cause misalignments of anything indented which is aligned
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: C coding style

Post by Brynet-Inc »

berkus wrote:
Brynet-Inc wrote:Tabs > Spaces.
Please, how will you enter them into the wiki? I'm interested.
Write the code in a text editor, compile test it, and then paste it. Surely nobody sane writes code directly on the Wiki.

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.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: C coding style

Post by Kevin »

NickJohnson wrote:I think the whole tabs/spaces thing is largely irrelevant, as long as tabs are not used for anything.
There, fixed that for you. ;)
Developer of tyndur - community OS of Lowlevel (German)
Darwin
Member
Member
Posts: 43
Joined: Sun Jan 16, 2011 6:58 pm
Location: United States

Re: C coding style

Post by Darwin »

NickJohnson wrote:I think the whole tabs/spaces thing is largely irrelevant, as long as tabs are not used for anything other than indentation...
As far as tabs and spaces go, this makes the most sense to me.
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: C coding style

Post by Chandra »

Brynet-Inc wrote:
berkus wrote:
Brynet-Inc wrote:Tabs > Spaces.
Please, how will you enter them into the wiki? I'm interested.
Write the code in a text editor, compile test it, and then paste it. Surely nobody sane writes code directly on the Wiki.
+1.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C coding style

Post by Solar »

Darwin wrote:
NickJohnson wrote:I think the whole tabs/spaces thing is largely irrelevant, as long as tabs are not used for anything other than indentation...
As far as tabs and spaces go, this makes the most sense to me.
Sadly enough, not really.

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
How easy is it to hit TAB twice at #1 and #2? How difficult to detect? How detrimental for layout should someone change the TAB representation width? (Above example isn't exactly great for that, but I didn't want to invest creativeness for the sake of an example right now.)

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? :oops:

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.
User avatar
Combuster
Member
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

Post by Combuster »

Do we really need to have a vote on this?
No we don't. Discussions of personal preference have already been excluded from changing the proposed style guide. :twisted:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: C coding style

Post by Love4Boobies »

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.
Combuster wrote:
Do we really need to have a vote on this?
No we don't. Discussions of personal preference have already been excluded from changing the proposed style guide. :twisted:
Except the style guide is more or less the preferences Solar and you had, just that no one really cared to impose their own. :P
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: C coding style

Post by quok »

Love4Boobies wrote:
Combuster wrote:
Do we really need to have a vote on this?
No we don't. Discussions of personal preference have already been excluded from changing the proposed style guide. :twisted:
Except the style guide is more or less the preferences Solar and you had, just that no one really cared to impose their own. :P
Ahem, I gave my preferences.They just happened to be aligned with what Solar and Combuster were saying. :)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C coding style

Post by Solar »

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.
As much as I would have loved that argument to be true :twisted: , it isn't. (Yes, I immediately whipped up my copy of the C99 standard.)

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).
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. :P
Guilty as charged.

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.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: C coding style

Post by Love4Boobies »

Solar wrote:
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.
As much as I would have loved that argument to be true :twisted: , it isn't. (Yes, I immediately whipped up my copy of the C99 standard.)

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).
Oops, I got excited for nothing :) That sentence starts with "Both the basic source and basic execution character sets"
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply