473,714 Members | 2,543 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 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.n et...
"Siemel Naran" <Si*********@RE MOVE.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.ini t]

1 The storage for objects with static storage duration
(_basic.stc.sta tic_) 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*********@re move.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)cybers pace.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
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
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...
1
9071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
1
6627
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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.