OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 5:12 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 70 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next
Author Message
 Post subject: Re: C coding style
PostPosted: Wed Jun 29, 2011 7:13 am 
Offline
Member
Member
User avatar

Joined: Fri Mar 07, 2008 5:36 pm
Posts: 2111
Location: Bucharest, Romania
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 ]


Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Wed Jun 29, 2011 12:04 pm 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 9:29 pm
Posts: 2426
Location: Canada
Tabs > Spaces.

_________________
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.


Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Wed Jun 29, 2011 12:43 pm 
Offline
Member
Member
User avatar

Joined: Tue Mar 23, 2010 3:01 pm
Posts: 228
Location: Uppsala, Sweden
*distributing pop corn*


Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Wed Jun 29, 2011 1:41 pm 
Offline
Member
Member
User avatar

Joined: Tue Mar 24, 2009 8:11 pm
Posts: 1249
Location: Sunnyvale, California
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.


Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Wed Jun 29, 2011 1:49 pm 
Offline
Member
Member
User avatar

Joined: Fri Jun 13, 2008 3:21 pm
Posts: 1700
Location: Cambridge, United Kingdom
Tabs for indents will cause misalignments of anything indented which is aligned


Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Wed Jun 29, 2011 2:20 pm 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 9:29 pm
Posts: 2426
Location: Canada
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.


Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Wed Jun 29, 2011 3:10 pm 
Offline
Member
Member

Joined: Sun Feb 01, 2009 6:11 am
Posts: 1070
Location: Germany
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)


Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Wed Jun 29, 2011 5:29 pm 
Offline
Member
Member

Joined: Sun Jan 16, 2011 6:58 pm
Posts: 43
Location: United States
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.


Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Wed Jun 29, 2011 6:22 pm 
Offline
Member
Member
User avatar

Joined: Sat Jul 17, 2010 12:45 am
Posts: 487
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 !


Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Thu Jun 30, 2011 12:00 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7612
Location: Germany
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:
// 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.

_________________
Every good solution is obvious once you've found it.


Last edited by Solar on Thu Jun 30, 2011 2:21 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Thu Jun 30, 2011 1:40 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
Quote:
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 ]


Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Thu Jun 30, 2011 2:34 am 
Offline
Member
Member
User avatar

Joined: Fri Mar 07, 2008 5:36 pm
Posts: 2111
Location: Bucharest, Romania
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:
Quote:
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 ]


Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Thu Jun 30, 2011 2:50 am 
Offline
Member
Member

Joined: Wed Oct 18, 2006 10:43 pm
Posts: 490
Location: Kansas City, KS, USA
Love4Boobies wrote:
Combuster wrote:
Quote:
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. :)


Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Thu Jun 30, 2011 3:09 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7612
Location: Germany
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.

_________________
Every good solution is obvious once you've found it.


Last edited by Solar on Thu Jun 30, 2011 4:02 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: C coding style
PostPosted: Thu Jun 30, 2011 3:30 am 
Offline
Member
Member
User avatar

Joined: Fri Mar 07, 2008 5:36 pm
Posts: 2111
Location: Bucharest, Romania
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 ]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 70 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 16 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group