GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ 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 16-May-2004, 02:13
BobbyMurcerFan BobbyMurcerFan is offline
Member
 
Join Date: May 2004
Posts: 103
BobbyMurcerFan is on a distinguished road

Style Guide Books??


I've found a few web documents that discuss naming and style conventions for C++, but are there any somewhat definitive books on the subject that any of you recommend?

There are so many style ?'s floating though my head? Should I start a class name w/ "C" or "T" or something else. Should I prefix variables w/ the data type, e.g. iWeight for an integer variable used for weight values. Should I intial capitalize my own function names? How about member funciton names? ... the list just goes on and on.

Any direction on C++ naming and style conventions and practices would be MUCH APPRECIATED. THANKS!
  #2  
Old 16-May-2004, 08:15
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 BobbyMurcerFan
I've found a few web documents that discuss naming and style conventions for C++, but are there any somewhat definitive books on the subject that any of you recommend?

There are so many style ?'s floating though my head? Should I start a class name w/ "C" or "T" or something else. Should I prefix variables w/ the data type, e.g. iWeight for an integer variable used for weight values. Should I intial capitalize my own function names? How about member funciton names? ... the list just goes on and on.

Any direction on C++ naming and style conventions and practices would be MUCH APPRECIATED. THANKS!

Hello. Welcome to GIDForums. This is a topic that has come up several times around here and I would bet that you would get a different opinion from everyone that posts here. I can tell from the code I see posted here that we all differ somewhat in our naming conventions.

What you need to decide is what works best for you but stays within some general bounds. Your code needs to be readable, not only by you but also others.

As for the type before the variable name? (I can't remember what this is called - I think it is named after somebody.) I am not a big fan of that myself. In your example, what if your program was expanding to allow a floating point for the variable weight? You have two choices, leave it as iWeight which makes matters worse or change your entire code to the variable name fWeight. So I think that there are some definite downsides to that.

I will provide several links of posts by other members as well as myself that make some good points about style
  • This is pretty close to the style I use. It is more of a C style, but I am more of a C programmer. As you can see from the discussion, not everyone agrees with this.
  • This is an excellent tutorial that talks about the formatting of code. Walt discusses several different options for styling here and covers some topics that are *critical* in getting readable code.
  • This is another tutorial by Walt about naming conventions. While, I differ slightly in my naming conventions, the important thing is the constistancy and the overall plan that he lays out.

I am not sure if this helps or not. If you are a professional programmer working for someone else, you may not have a whole lot of choice in some of these matters. If you are fortunate enough to be a free lancer or hobbyist, you can make your own style that fits you best.

You need to pick a style that makes the most sense to you, while trying to keep your program world-readable. Look through some of the code that is posted here and try to see what is the most readable to your eyes.

If you have any more links or comments that you think are rather good, I would be interested in hearing about them.

Cheers,
d
  #3  
Old 16-May-2004, 10:44
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
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 BobbyMurcerFan
I've found a few web documents that discuss naming and style conventions for C++, but are there any somewhat definitive books on the subject that any of you recommend?

There are so many style ?'s floating though my head? Should I start a class name w/ "C" or "T" or something else. Should I prefix variables w/ the data type, e.g. iWeight for an integer variable used for weight values. Should I intial capitalize my own function names? How about member funciton names? ... the list just goes on and on.

Any direction on C++ naming and style conventions and practices would be MUCH APPRECIATED. THANKS!
Yeah, what he said. :-)

I can't remember the name either of the prefix -- it always reminded me of an animal like hippo- something, but I digress.

Using the prefix can be very useful in certain situations. In VB I use it all the time got the control and features (forms, boxes, labels, etc) but rarely for variables. MFC and the M$ API use them all the time and are quite handy there also. So when you interface with large systems like MFC, Oracle, and the like, it's extremely useful to have that style of definition. For most programs though it's sometimes overkill. But again if you are writing an extremely complex and large program/system, they may stop many headaches. It will allow you to use the 'same' variable name in different code sections without fear of mixing them up.

But as to dsmith's allegation about changing iWeight to fWeight, that's pretty mich a non-issue with today's editors and IDEs IMHO.

As I mentioned in the tutorial -- or maybe I didn't -- my style is:

Variables
  • initial lowercase: segtype
  • sometimes capitalize 2nd/3rd words for clarity: segType
Function Names
  • upper/lower case: SegTypeValue()
Defines
  • upper case: MAXBUFSIZE
As far as classes, I don't have a set style yet. Like d I'm more of a C programmer. I'll sometimes uppercase them, or have a hybrid mix, like FRACTdefinition, uppercase the first word then do whatever with the rest.

But methods are just functions so they follow the upper/lower style.

Develop your style -- sometimes it takes years to fall into a pattern. I know I've changed my style many times over the years.
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #4  
Old 16-May-2004, 12:25
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
But as to dsmith's allegation about changing iWeight to fWeight, that's pretty mich a non-issue with today's editors and IDEs IMHO.

Very true. Hell, even notepad has a Search/Replace function. But it does take discipline (of which I have none). So in some (most?) cases these variable names would not be changed and add to the confusion.

See, what I mean about a million styles? Walt and I have pretty similar coding styles and we still can't agree.
  #5  
Old 16-May-2004, 13:19
BobbyMurcerFan BobbyMurcerFan is offline
Member
 
Join Date: May 2004
Posts: 103
BobbyMurcerFan is on a distinguished road
Thanks a lot guys! BTW, I started out w/ Access, so I'm a little used to Stan Leszynski and Greg Reddick's convention they created. Doing some Google searches, I've found it was based on Hungarian Notation devised at Microsoft for C/C++.
 


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
complete e-marketing guide primelibra Search Engine Optimization Forum 4 01-Aug-2004 20:45
IRC Services Admin help guide Allowee Open Discussion Forum 2 25-Apr-2004 08:32
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59
C++ books for newbie mak90thug C++ Forum 4 04-Feb-2004 15:58
Video Card FAQ & Overclocking Guide (Updated) asanthadenz Computer Hardware Forum 2 05-Mar-2003 19:37

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:17.


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