473,714 Members | 2,540 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3374
"Pete Vidler" <pv*****@mailbl ocks.com> wrote
Claudio Puviani wrote:
[snip]
I've heard of this one, but isn't it seriously out of date?
Good information is never out of date. Knuth's "Art
of Computer Programming" series was first published
in 1968 and anyone would get shot for calling it out
of date. A discriminating reader can easily filter out
what's still relevant and what isn't. Those who can't
are probably not ready to read the book yet.


Right, but if it almost pre-dates namespaces there is
going to be a lot missing from it -- am I right?
Templates and such?


First of all, it doesn't predate namespaces, much less templates. It's not a
book about individual C++ features. If that's what you're looking for, this
is the wrong book for you. It's about large-scale software design, and the
basic principles can be applied to almost any language.
Knuth's book is somewhat different, the concepts it
addresses are not based on a language that has changed
a great deal over time. It isn't even based on a real
language at all (unless you count a few implementations
by enthusiasts).
Software design is fairly language-independent as well. John Lakos simply
used C++ as an example because it's still the best vehicle for large scale
software development.
I've also heard that he has some strange guidelines like
not using namespaces?


EVERY book that proposes guidelines has strange guidelines.
Or at least, we perceive them as being strange because the
author comes from a different background. The thing to
remember is that guidelines are NOT rules and should be
followed only if they make sense in a given context.

[snip]

I have to disagree. I have read many guideline type books (the
"Effective" and "Exceptiona l" books to name five) that I have
had no disagreements with at all. I don't believe I come from
the same background as the authors.


You're a rare exception. Most people have built up equally valid guidelines
over years of developing in C++, and it's just impossible for every rule in
every book to seemlessly fit into every other system. It's not a criticism
of the authors; just situations that make some recommendations inapplicable
or inconsistent with existing practices.
The truth is that whether or not one appreciates John's style
or agrees with every detail, anyone who hasn't read the book
is a step behind those who have with regard to physical design.

[snip]

That cannot possibly be true. If we haven't read the book we
might still know everything it contains (through other sources)
and not realise it.


That's highly unlikely and slightly pretentious. The best software
developers I know -- and I know some stellar ones -- still learn something
every time they pick up a new book. Anyone who thinks he's too good to learn
something new is just plain delusional.
And for all I know, if I do read it much of its contents might be
obvious to me.


Right. Maybe you should read it instead of patting yourself on the back
prematurely.

Claudio Puviani
Jul 22 '05 #41
Phlip wrote in news:cL******** ********@newssv r32.news.prodig y.com in
comp.lang.c++:
Joe Gottman wrote:
If you have code like
class D : public B {};
B * pb = new D();
delete pb;

and D does not have a virtual destructor, undefined behavior results.

There
are two possible solutions to this. If you give B a virtual
destructor, the correct destructor is called by delete.
Alternatively, if you give B a protected destructor the "delete pb;"
statement will fail to compile.

Thus,
you give a class a protected destructor when you don't want anybody

deleting
a derived class through a pointer to the base class.


The rules for 'protected' roughly mean "anything you could reference
thru 'this' (plus statics)".

So 'delete this' breaks your protection.


It calls D's dtor, since that is the type of *this.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #42
Phlip wrote in news:cL******** ********@newssv r32.news.prodig y.com in
comp.lang.c++:
Joe Gottman wrote:
If you have code like
class D : public B {};
B * pb = new D();
delete pb;

and D does not have a virtual destructor, undefined behavior results.

There
are two possible solutions to this. If you give B a virtual
destructor, the correct destructor is called by delete.
Alternatively, if you give B a protected destructor the "delete pb;"
statement will fail to compile.

Thus,
you give a class a protected destructor when you don't want anybody

deleting
a derived class through a pointer to the base class.


The rules for 'protected' roughly mean "anything you could reference
thru 'this' (plus statics)".

So 'delete this' breaks your protection.


It calls D's dtor, since that is the type of *this.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #43
Pete Vidler wrote:
Claudio Puviani wrote:
[snip]
It has rudimentary design guidelines as well. The formatting section is
just a few pages, and the least interesting at that.


But how rudimentary? I can't possibly buy a book on the off-chance it
might contain what I need (I couldn't afford it). I can't find any
actual reviews of the book, and the only info in the summary suggets the
book is about formatting/naming conventions (useless to me).
Here's a list of books I posted once before that would give you what you
need:


Cool, thanks. I have read most of these, but there are one or two on
that list I haven't seen before.

-- Pete


I never bought that particular book, but I have the Java counterpart. I
assume they are comperable. It's probably not the one book you are looking
for. OTOH, there is something to be said for the relationship between
programming style and design approach. To some extent good style produces
good design by default.

One of the best lessons I ever learned was something someone told me about
how to stack boxes in a warehouse. If there is some obvious pattern
available follow it. Things will simply fall into place. So, you stack
all the boxes so the same face (the picture of the product, for example) is
oriented identically for all boxes. The principle has far reaching
consequence on how smoothely the overall processes work.

But, I believe you are looking for just about the same kind of resource I
have been wanting. Have you found a winner yet?
--
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 #44
Pete Vidler wrote:
Claudio Puviani wrote:
[snip]
It has rudimentary design guidelines as well. The formatting section is
just a few pages, and the least interesting at that.


But how rudimentary? I can't possibly buy a book on the off-chance it
might contain what I need (I couldn't afford it). I can't find any
actual reviews of the book, and the only info in the summary suggets the
book is about formatting/naming conventions (useless to me).
Here's a list of books I posted once before that would give you what you
need:


Cool, thanks. I have read most of these, but there are one or two on
that list I haven't seen before.

-- Pete


I never bought that particular book, but I have the Java counterpart. I
assume they are comperable. It's probably not the one book you are looking
for. OTOH, there is something to be said for the relationship between
programming style and design approach. To some extent good style produces
good design by default.

One of the best lessons I ever learned was something someone told me about
how to stack boxes in a warehouse. If there is some obvious pattern
available follow it. Things will simply fall into place. So, you stack
all the boxes so the same face (the picture of the product, for example) is
oriented identically for all boxes. The principle has far reaching
consequence on how smoothely the overall processes work.

But, I believe you are looking for just about the same kind of resource I
have been wanting. Have you found a winner yet?
--
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 #45
Pete Vidler wrote:
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

Zawinski is known for being a bit off the wall, but he also wrote everything
from the keycaps program, through a good deal of the XEmacs code, and to a
lot of the code in the original Mozilla (aka Netscape). Consider the love
affair between Netscape and Microsoft when you read his review of _Writing
Solid Code_:

http://www.mozillazine.org/resources...ndations1.html

I haven't read any of the books cover to cover, and cannot provide
assessments of the reviews. It just seemed like a resource worth sharing.
--
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 #46
Pete Vidler wrote:
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

Zawinski is known for being a bit off the wall, but he also wrote everything
from the keycaps program, through a good deal of the XEmacs code, and to a
lot of the code in the original Mozilla (aka Netscape). Consider the love
affair between Netscape and Microsoft when you read his review of _Writing
Solid Code_:

http://www.mozillazine.org/resources...ndations1.html

I haven't read any of the books cover to cover, and cannot provide
assessments of the reviews. It just seemed like a resource worth sharing.
--
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 #47
Claudio Puviani wrote:
[snip]
Right, but if it almost pre-dates namespaces there is
going to be a lot missing from it -- am I right?
Templates and such?
First of all, it doesn't predate namespaces, much less templates. It's not a
book about individual C++ features. If that's what you're looking for, this
is the wrong book for you. It's about large-scale software design, and the
basic principles can be applied to almost any language.


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.

[snip] Software design is fairly language-independent as well. John Lakos simply
used C++ as an example because it's still the best vehicle for large scale
software development.
I'm not looking for software design info (large scale or otherwise), but
guidelines on using C++ effectively.

[snip] You're a rare exception. Most people have built up equally valid guidelines
over years of developing in C++, and it's just impossible for every rule in
every book to seemlessly fit into every other system. It's not a criticism
of the authors; just situations that make some recommendations inapplicable
or inconsistent with existing practices.


Fair enough.

[snip]
The truth is that whether or not one appreciates John's style
or agrees with every detail, anyone who hasn't read the book
is a step behind those who have with regard to physical design.


That cannot possibly be true. If we haven't read the book we
might still know everything it contains (through other sources)
and not realise it.


That's highly unlikely and slightly pretentious. The best software
developers I know -- and I know some stellar ones -- still learn something
every time they pick up a new book. Anyone who thinks he's too good to learn
something new is just plain delusional.


Did I say I thought I was too good to learn something new? Would I have
even sent my original post if I thought that?

I don't care how good someone is at software development, even beginners
could pick up a book and find they already know the info it contains.
It's a distinct possibility with so many books, articles, web pages, etc
covering the same topics.

I'm fairly sure I could read a beginners C++ guide and learn nothing
that I didn't already know. That doesn't mean that I believe this of all
books.

Note that none of this reflects on the book in question. I have not read
it so I cannot possibly say. My purpose in asking these questions about
it was not to insult anyone, or to imply that it is not worth reading --
I am just trying to establish if it meets my needs.
And for all I know, if I do read it much of its contents might be
obvious to me.


Right. Maybe you should read it instead of patting yourself on the back
prematurely.


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
Jul 22 '05 #48
Claudio Puviani wrote:
[snip]
Right, but if it almost pre-dates namespaces there is
going to be a lot missing from it -- am I right?
Templates and such?
First of all, it doesn't predate namespaces, much less templates. It's not a
book about individual C++ features. If that's what you're looking for, this
is the wrong book for you. It's about large-scale software design, and the
basic principles can be applied to almost any language.


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.

[snip] Software design is fairly language-independent as well. John Lakos simply
used C++ as an example because it's still the best vehicle for large scale
software development.
I'm not looking for software design info (large scale or otherwise), but
guidelines on using C++ effectively.

[snip] You're a rare exception. Most people have built up equally valid guidelines
over years of developing in C++, and it's just impossible for every rule in
every book to seemlessly fit into every other system. It's not a criticism
of the authors; just situations that make some recommendations inapplicable
or inconsistent with existing practices.


Fair enough.

[snip]
The truth is that whether or not one appreciates John's style
or agrees with every detail, anyone who hasn't read the book
is a step behind those who have with regard to physical design.


That cannot possibly be true. If we haven't read the book we
might still know everything it contains (through other sources)
and not realise it.


That's highly unlikely and slightly pretentious. The best software
developers I know -- and I know some stellar ones -- still learn something
every time they pick up a new book. Anyone who thinks he's too good to learn
something new is just plain delusional.


Did I say I thought I was too good to learn something new? Would I have
even sent my original post if I thought that?

I don't care how good someone is at software development, even beginners
could pick up a book and find they already know the info it contains.
It's a distinct possibility with so many books, articles, web pages, etc
covering the same topics.

I'm fairly sure I could read a beginners C++ guide and learn nothing
that I didn't already know. That doesn't mean that I believe this of all
books.

Note that none of this reflects on the book in question. I have not read
it so I cannot possibly say. My purpose in asking these questions about
it was not to insult anyone, or to imply that it is not worth reading --
I am just trying to establish if it meets my needs.
And for all I know, if I do read it much of its contents might be
obvious to me.


Right. Maybe you should read it instead of patting yourself on the back
prematurely.


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
Jul 22 '05 #49
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 #50

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

Similar topics

22
2210
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
45084
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 cells and lines in the top/left/right and bottom cells (sounds odd, you'll see what i mean when you see the site). It works fine, except the top and bottom rows are a lot bigger than i've specified when they should be flush with the centre cell as...
61
669
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, from various sources. - Non-leaf classes should be abstract (have pure virtual methods), from More Effective C++, Item 33.
16
2401
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 underscore suffix. class SomeClass { private:
39
2202
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 ? style A: .... .... int a = 1; if(0==a) {
3
2374
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 variables treated the same as parameters (i.e., camel case)? I notice that Hungarian notation is being done away with. Also, is it good practice to use an underscore to begin the name of a field? I've seen that, and it seems like a good idea,...
5
1198
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 application project in 2004, based on our C# guidelines. http://www.galasoft-lb.ch/myjavascript/Siemens_SBT_JavaScript_Coding_Guidelines.pdf Please note the following:
8
2289
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 behind going for a different convention in .NET. The recommendations do make sense (to me, at least but I may be wrong and would like someone to correct me). Scope Prefixes Member Variable Prefix: Most of the people still use (and like the idea...
0
8796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9170
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9009
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7946
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4462
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2105
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.