473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

C++ Guidelines

Hi Folks,

I'm wondering if there is a compilation of C++ guidelines out there
anywhere. Here are some of the ones I've picked up so far (as examples):

- Functions should fit on one screen, from various sources.

- Non-leaf classes should be abstract (have pure virtual methods), from
More Effective C++, Item 33.

- Virtual methods should be private by default and protected if they
need access to a base classes version (except for the destructor, of
course), from http://www.gotw.ca/publications/mill18.htm.

- Header files should be self contained, from various sources.

- Destructors for base classes should be either virtual or protected.

I think I've probably missed (or never heard of) quite a few more.
Anyone know where I can find such things? Or have some guidelines of
their own to share?

-- Pete
Jul 22 '05
64 3281
Steven T. Hatton wrote:
[snip]
But, I believe you are looking for just about the same kind of resource I
have been wanting. Have you found a winner yet?


Of the books I have read so far, the following have been extremely useful:

Effective C++
More Effective C++
Effective STL
Exceptional C++
More Exceptional C++

.... not exactly guideline books, but still good: TC++PL, Design
Patterns, Modern C++ Design, etc.

And the articles at www.gotw.ca are good. Also, there are many good
articles at www.cuj.com (if you dig around the archives).

Other than that I have found very little.

-- Pete
Jul 22 '05 #51
Joe Gottman wrote:
[snip]
- Destructors for base classes should be either virtual or protected.


Never heard of the "protected" one.

[snip]

Neither had I, which was /exactly/ why I started this thread. To try and
find more of these guidelines.

-- Pete
Jul 22 '05 #52
Joe Gottman wrote:
[snip]
- Destructors for base classes should be either virtual or protected.


Never heard of the "protected" one.

[snip]

Neither had I, which was /exactly/ why I started this thread. To try and
find more of these guidelines.

-- Pete
Jul 22 '05 #53
Pete Vidler wrote:
Joe Gottman wrote:
[snip]
- Destructors for base classes should be either virtual or protected.

Never heard of the "protected" one.

[snip]

Neither had I, which was /exactly/ why I started this thread. To try and
find more of these guidelines.

-- Pete

Here's one I tend to use in Java, and believe it applies to C++ as well. In
C++ terms it would be use /out/ not /std::cout/. That is, create a
variable /out/ of type std::ostream. Do this, say in the parameter list of
a function. Then when you call the function you pass std::cout, or some
other instance of std::ostream.

Unfortunately, I am not familiar enough with the STL <iostream> to know
exactly what level of abstraction to use. E.g., there is a distinction
between std::cout, and std::wcout.

The same also applies to input streams.

The idea is to write code that doesn't care what it's writing to/reading
from, just so long as it has a stream. This is handy, for example, when
you write code that can be run from the commandline and dumps to console,
or can be run from a web server and writes to the network.

--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #54
Pete Vidler wrote:
Joe Gottman wrote:
[snip]
- Destructors for base classes should be either virtual or protected.

Never heard of the "protected" one.

[snip]

Neither had I, which was /exactly/ why I started this thread. To try and
find more of these guidelines.

-- Pete

Here's one I tend to use in Java, and believe it applies to C++ as well. In
C++ terms it would be use /out/ not /std::cout/. That is, create a
variable /out/ of type std::ostream. Do this, say in the parameter list of
a function. Then when you call the function you pass std::cout, or some
other instance of std::ostream.

Unfortunately, I am not familiar enough with the STL <iostream> to know
exactly what level of abstraction to use. E.g., there is a distinction
between std::cout, and std::wcout.

The same also applies to input streams.

The idea is to write code that doesn't care what it's writing to/reading
from, just so long as it has a stream. This is handy, for example, when
you write code that can be run from the commandline and dumps to console,
or can be run from a web server and writes to the network.

--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #55
Pete Vidler wrote:
What I am looking for is what I posted originally -- guidelines similar
to the ones I posted. I'm not looking for a book describing individual
C++ features, but rather a book describing (the authors) best practices
in /using/ them.
This thread has revolved around such books.
I'm not looking for software design info (large scale or otherwise), but
guidelines on using C++ effectively.


You are having reading comprehension issues. Lakos' book is about physical
construction - the do's and don't's of implementation. Its forward expressly
states it is not about "design" in the abstract.

Stop complaining about everyone's quite valid recommendations.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
Jul 22 '05 #56
Pete Vidler wrote:
What I am looking for is what I posted originally -- guidelines similar
to the ones I posted. I'm not looking for a book describing individual
C++ features, but rather a book describing (the authors) best practices
in /using/ them.
This thread has revolved around such books.
I'm not looking for software design info (large scale or otherwise), but
guidelines on using C++ effectively.


You are having reading comprehension issues. Lakos' book is about physical
construction - the do's and don't's of implementation. Its forward expressly
states it is not about "design" in the abstract.

Stop complaining about everyone's quite valid recommendations.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
Jul 22 '05 #57
"Pete Vidler wrote:
Never heard of the "protected" one.
Neither had I, which was /exactly/ why I started this thread. To try and
find more of these guidelines.


Did you read the C++ FAQ?

You have read enough to start coding. If you only do the intersection - not
the union - of those books' recommendations, you will have a sufficiently
narrow sane subset.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
Jul 22 '05 #58
Phlip wrote:
"Pete Vidler wrote:
>Never heard of the "protected" one.
Neither had I, which was /exactly/ why I started this thread. To try and
find more of these guidelines.


Did you read the C++ FAQ?


That came out wrong.

Did you read the _book_ /C++ FAQs/ by Marshall Cline?
You have read enough to start coding. If you only do the intersection - not the union - of those books' recommendations, you will have a sufficiently
narrow sane subset.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces

Jul 22 '05 #59

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:Lu******************@bgtnsc05-news.ops.worldnet.att.net...
- Non-leaf classes should be abstract (have pure virtual methods), from
More Effective C++, Item 33.
A suggestion. Sometimes we want a reasonable default behavior which

clients can then override. Requring the above as a guideline imposes too much
design burden on the project and could cause it to run out of budget.


Agreed. I don't like that "guideline" very much.
- Header files should be self contained, from various sources.


What?


Makes sense to me, and I wholeheartedly agree. Nothing worse than
unnecessary dependencies in header files.
Jul 22 '05 #60

"Pete Vidler" <pv*****@mailblocks.com> wrote in message
news:OV************@newsfe1-gui.server.ntli.net...

A suggestion. Functions could easily be longer once you add blank lines and comments. Functions that are pretty straightforward but long, say because they have to read in a lot of input arguments, are fine, it seems to me. As soon as you start to process input arguments and do stuff, that's when the long function should raise a bell.

A related concept is that functions should have few input arguments.


I usually adhere to this one because I find that I make fewer mistakes
if the entire method is immediately available. I'm not sure how this
relates to the number of arguments, but I'll accept that as a suggestion.


Worry about cohesion rather than number of lines. If you worry too much
about number of lines, then you'll start breaking down every 2 lines into
function, and that can make things *less* readable ultimately.
Jul 22 '05 #61
"jeffc" <no****@nowhere.com> wrote in message
news:40********@news1.prserv.net...
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message

- Header files should be self contained, from various sources.


What?


Makes sense to me, and I wholeheartedly agree. Nothing worse than
unnecessary dependencies in header files.


You mean like header A.h fails to include B.h though it depends on entities
from B.h. Then the user who includes A.h has to include B.h first then A.h.
Is this what you mean? If so, definitely this is a good rule.

In fact, my cpp files always include the h file as the first non-comment
line. But in windows they have this stdafx.h if you use pre-compiled
headers, and so you shouldn't by accident include B.h in this file.
Jul 22 '05 #62
Pete Vidler wrote:
I am not patting myself on the back. I do not believe it will be obvious
to me. It's just that the book has some /very/ mixed reviews on the
internet, so I need some solid reassurance that it covers the kind of
guidelines that I mentioned earlier before I go out and buy it.

-- Pete

I got to #3, and so far, I'm finding these worth considering. I suggest
reading with your eyes open. #3 threw me for a loop at first.

The Top 20 C++ Tips of All Time
http://www.devx.com/cplus/Article/16328/0/page/1
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #63
Steven T. Hatton wrote:
Pete Vidler wrote:
I am not patting myself on the back. I do not believe it will be obvious
to me. It's just that the book has some /very/ mixed reviews on the
internet, so I need some solid reassurance that it covers the kind of
guidelines that I mentioned earlier before I go out and buy it.

-- Pete I got to #3, and so far, I'm finding these worth considering. I suggest
reading with your eyes open. #3 threw me for a loop at first.


Doh! #2 is the one that tripped me up.
The Top 20 C++ Tips of All Time
http://www.devx.com/cplus/Article/16328/0/page/1


Can I trust #4? I believe I can by virtue of this:
http://www.itga.com.au/~gnb/wp/cd2/
3.6.2 Initialization of non-local objects [basic.start.init]

1 The storage for objects with static storage duration
(_basic.stc.static_) shall be zero-initialized (_dcl.init_) before any
other initialization takes place. Objects of POD types
(_basic.types_) with static storage duration initialized with constant
expressions (_expr.const_) shall be initialized before any dynamic
initialization takes place. Objects of namespace scope with static
storage duration defined in the same translation unit and dynamically
initialized shall be initialized in the order in which their defini-
tion appears in the translation unit. [Note: _dcl.init.aggr_
describes the order in which aggregate members are initialized. The
initialization of local static objects is described in _stmt.dcl_. ]

But I'm not sure if there are any subtelties I need to be aware of.
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #64
Siemel Naran <Si*********@remove.att.net> spoke thus:
In fact, my cpp files always include the h file as the first non-comment
line. But in windows they have this stdafx.h if you use pre-compiled
headers, and so you shouldn't by accident include B.h in this file.


If one writes header files correctly, including header files
accidently causes very minor effects at worst.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #65

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: beliavsky | last post by:
Is there a more recent set of Python style guidelines than PEP 8, "Style Guide for Python Code", by van Rossum and Warsaw, at http://www.python.org/peps/pep-0008.html , which is dated July 5, 2001?
6
by: stevehayter | last post by:
Wonder if anyone can help me solve this problem! I have a 3x3 table which i'm using to create a table with a rounded edge using images in the top left, top right, bottom left, and bottom right...
61
by: Pete Vidler | last post by:
Hi Folks, I'm wondering if there is a compilation of C++ guidelines out there anywhere. Here are some of the ones I've picked up so far (as examples): - Functions should fit on one screen,...
16
by: E. Robert Tisdale | last post by:
C++ Programming Style Guidelines http://geosoft.no/development/cppstyle.html I think that these guidelines are almost *all* wrong. For example: 11. Private class variables should have...
39
by: jamilur_rahman | last post by:
What is the BIG difference between checking the "if(expression)" in A and B ? I'm used to with style A, "if(0==a)", but my peer reviewer likes style B, how can I defend myself to stay with style A...
3
by: John Salerno | last post by:
Does Microsoft have any naming guidelines for variables? I've been reading their guidelines for just about every other type (methods, parameters, properties, etc.) but nothing about variables. Are...
5
by: Laurent Bugnion [MVP] | last post by:
Hi group, In agreement with the head of our R&D department, I published my firm's JavaScript Coding Guidelines. I work for Siemens Building Technologies. We developed these guidelines for a web...
8
by: mrashidsaleem | last post by:
Can anyone guide me what is wrong with the naming conventions suggested below? I know that this is not recommended by many (including Microsoft) but I need to know what exactly is the rationale...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.