GIDForums  

Go Back   GIDForums > Computer Programming Forums > Miscellaneous Programming Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Feb-2004, 12:42
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light

C Coding Style


I am in the process of upgrading my linux kernel and I came upon this file that I found extremely interesting.

This (attached file) is the linux kernel maintaners view of the proper syntax for C. I found this very enlightening and thought it may be a good source for others as well. This matches my coding style. Not only that, but the items where I vary, I actually feel that this is a better style approach than mine.

It is interesting that they mention Kernighan and Ritchie, as this was the authors of the first book that I learned C from and as mentioned in the file, the prophets of the C language. I think that they would faint if they saw some of the modern C/C++ code being written.

To clarify, C++ is not C. C++ is an OO language first and foremost. Therefore much of the coding style around C++ is created from its OO relatives and not from the original C. I am not bashing C++, quite the opposite, I absolutely love some of the features of C++. But I am questioning the deviation from C syntax highlighting.

One more thing. If you are being taught C++ or C, the syntax style that your professor prefers is the correct one at that time. You can set your own style as you work on your own projects.

I would love to hear differing opinions on this. I am not posting this to say this is the way everyone should code, but rather to open a dialogue about different coding styles.
Attached Files
File Type: txt CodingStyle.txt (10.7 KB, 271 views)
  #2  
Old 22-Feb-2004, 19:55
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
That is one MESSY text file. Could you please attach one that is more organized and easy to read?
  #3  
Old 22-Feb-2004, 20:56
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by aaroncohn
That is one MESSY text file. Could you please attach one that is more organized and easy to read?

Actually, no, I can't. It is not mine and I don't particularly want to rewrite it. Remember, it is a "text" file, not an RTF or a formatted file. And quite frankly for a text file, I find it extremely readable.

My suspicion is that you are using Windows text editor with no word wrap. My suggestion is to use wordpad with word wrap (which I believe is a default) as it recognizes both the *nix way for a newline as well as the windows way for a newline.
  #4  
Old 22-Feb-2004, 21:34
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Well you're right in one aspect. I am using notepad to view it. I only use my Fedora installation to mess around and learn various things. I'll be using it more once I start unix programming, though.

edit: opened in it wordpad. MUCH better. thanks for the tip
  #5  
Old 23-Feb-2004, 00:17
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
I do have a gripe with the info in the coding style text file. And that is it's blind acceptance as in:
Quote:
The other issue that always comes up in C styling is the placement of braces. Unlike the indent size, there are few technical reasons to choose one placement strategy over the other, but the preferred way, as shown to us by the prophets Kernighan and Ritchie, is to put the opening brace last on the line, and put the closing brace first, thusly:
CPP / C++ / C Code:
if (x is true) {
    we do y
}
...
Heretic people all over the world have claimed that this inconsistency is ... well ... inconsistent, but all right-thinking people know that (a) K&R are _right_ and (b) K&R are right... (last I looked this wasn't Cuba)

Note that the closing brace is empty on a line of its own, _except_ in the cases where it is followed by a continuation of the same statement, ie a "while" in a do-statement or an "else" in an if-statement, like this: (this I agree with)
CPP / C++ / C Code:
do {
    body of do-loop
} while (condition);
and (this I don't)
CPP / C++ / C Code:
if (x == y) {
    ..
} else if (x > y) {
    ...
} else {
    ....
}
Rationale: K&R. (where's my kool-aid?)

Also, note that this brace-placement also minimizes the number of empty (or almost empty) lines, without any loss of readability (are they reading the same code I am?). Thus, as the supply of new-lines on your screen is not a renewable resource (think 25-line terminal screens here (yeah, let's keep up the legacy of the 1980's, why move forward)), you have more empty lines to put comments on.
Raising K&R to the level of prophets is a little bizarre. Since when are programmers supposed to blindly follow a concept simply because K&R said so? At least in programming style. For basic information, OK. But for coding style, what makes them right? I know some people's religion is programming, but really! Programmers are supposed to be thinkers, not followers!

And I completely disagree with the statement about readability. I find the following much easier to read:
CPP / C++ / C Code:
if (x is true) 
{
    we do y
}

if (x == y) 
{
    ..
} 
else 
if (x > y) 
{
   ...
} 
else 
{
    ....
}
Yes, it takes more lines, but so what? The flow to me is more obvious...

My bottom line is decide for yourself what is correct for your style. Try my way, try K&R's way, make up your own way. Then decide what makes sense to you -- keeping in mind other people may have to read you code too. So use some common sense.
  #6  
Old 23-Feb-2004, 06:36
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
I TOTALLY agree with you WaltP I didn't want to say anything before today because C and me don't jive - yet! However PHP and C are so similar and when I write my PHP codes I ALWAYS do this:

PHP Code:

if( $condition )
{
  // do something;
}
else
{
  // do something else;
} 



I never figured out why people generally preferred it the other way...
  #7  
Old 23-Feb-2004, 07:50
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by WaltP
My bottom line is decide for yourself what is correct for your style. Try my way, try K&R's way, make up your own way. Then decide what makes sense to you -- keeping in mind other people may have to read you code too. So use some common sense.

On this we agree 100%. The main thing is to think about style and readability and then come up with a style that suits you.

While the placement of my brackets and if/else statements is exactly like that discussed in the file (and in my opinion, it is the more readable way ), I disagree with their use of 8 spaces for a tab. I agree with the rational (shorter, less complicated functions) but not the practice.

Probably, the biggest reason that I posted it was its discussion of mixed case variables and function names. This is a personal pet peeve of mine and I thought I may be the only one in the *modern* programming world that cringes everytime I see a mixed case variable. Once again, this is my opinion and not something I am preaching.

Walt, one thing to keep in mind when reading this file, is that it is presented somewhat tounge in cheek. There is no need to take this file offensively or even follow its suggestions. (unless you are going to be contributing to the linux kernel sometime soon. )
  #8  
Old 23-Feb-2004, 08:05
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Yeah, I agree with Walt about the braces. The file did have some good advice, but I was really astonished at how the file tried to forcefully tell its readers that there was only one right way to do braces :-P
  #9  
Old 23-Feb-2004, 12:17
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dsmith
Walt, one thing to keep in mind when reading this file, is that it is presented somewhat tounge in cheek. There is no need to take this file offensively or even follow its suggestions. (unless you are going to be contributing to the linux kernel sometime soon. )
I agree, and wasn't offended. This file was obviously not meant for the general populace, but for the Linux few. I was being tongue-in-cheek too and elected no to use 's within the quote -- probably should have in retreospect. But at 4AM some decisions are hard to make

By the way, speaking of smileys, what significance are and ? Are they just for fun or do they have a deeper meaning? I kinda like the hairy one. Looks like me in the morning...
  #10  
Old 23-Feb-2004, 14:08
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
That banana was my AIM buddy icon for quite awhile.
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
coding a word with a givin factor funnyf CPP / C++ Forum 2 13-Jan-2004 08:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:34.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.