473,702 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ Coding Standards

http://www.cdbook.cn/book.asp?id=2393
Organizational and Policy Issues
1
0. Don't sweat the small stuff. (Or: Know what not to standardize.)
2
1. Compile cleanly at high warning levels.
4
2. Use an automated build system.
7
3. Use a version control system.
8
4. Invest in code reviews.
9

Design Style
11
5. Give one entity one cohesive responsibility
12
6. Correctness, simplicity, and clarity come first
13
7. Know when and how to code for scalability.
14
8. Don't optimize prematurely.
16
9. Don't pessimize prematurely.
18
10. Minimize global and shared data.
19
11. Hide information.
20
12. Know when and how to code for concurrency.
21
13. Ensure resources are owned by objects. Use explicit RAII and
smart pointers. 24

Coding Style
27
14. Prefer compile- and link-time errors to run-time errors.
28
15. Use const proactively.
30
16. Avoid macros.
32
17. Avoid magic numbers.
34
18. Declare variables as locally as possible.
35
19. Always initialize variables.
36
20. Avoid long functions. Avoid deep nesting.
38
21. Avoid initialization dependencies across compilation units.
39
22. Minimize definitional dependencies. Avoid cyclic dependencies.
40
23. Make header files self-sufficient.
42
24. Always write internal #include guards. Never write external
#include guards. 43
http://www.cdbook.cn/book.asp?id=2393

Apr 29 '06 #1
3 3345
ct***@163.com schrieb:
<snip: cn-URL advertising a translated version of "C++ Coding Standards"
and an excerpt of the list of rules>

While Herb and Andrei have done excellent work and the book
can serve as company C++ coding guideline or standard or
provide a good base for one, this has to do nothing whatsoever
with C. In fact, some of the more detailled rules do not make
sense or may even be harmful when applied to C.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 29 '06 #2
Michael Mair a écrit :
ct***@163.com schrieb:
<snip: cn-URL advertising a translated version of "C++ Coding Standards"
and an excerpt of the list of rules>

While Herb and Andrei have done excellent work and the book
can serve as company C++ coding guideline or standard or
provide a good base for one, this has to do nothing whatsoever
with C. In fact, some of the more detailled rules do not make
sense or may even be harmful when applied to C.

Cheers
Michael


Most of those rules apply to C too. The ones that are C++ specific are:
38 (initialization dependencies).
This is for C++ constructors stuff. Mostly irrelevant in C.
30 (Avoid macros).
In standard C is not possible to replace
#define MAX_SIZE 512
with
const int MAX_SIZE=512;
Lcc-win32 will accept the C++ semantics when you write
static const int MAX_SIZE=512;
In a C context I need the static to ensure the variable is
not accessed anywhere else. In this particular case lcc-win32
will just replace all occurrences of MAX_SIZE with 512
as it was a macro.
28 Use const proactively.
C++ "const" stuff is obscure and can be ignored in C. I
think this simplifies the language, at least it produces
less headaches to me :-)
21 (Ensure resources are owned by objects).
C is not object oriented, so this makes no sense, since
no destructors exists.

The other rules are quite sensible
Organizational and Policy Issues
1
0. Don't sweat the small stuff. (Or: Know what not to standardize.)
2
1. Compile cleanly at high warning levels.
4
2. Use an automated build system.
7
3. Use a version control system.
8
4. Invest in code reviews.
9

All of those rules make sense in C, Python, Ada, Visual Basic...
They are just common sense.

Design Style
11
5. Give one entity one cohesive responsibility

I am not sure what this means in C, probably that you have one function
that organizes the software module calling the others, managing the
shared data, etc.

This is too abstract to be of any use.

12
6. Correctness, simplicity, and clarity come first
13
7. Know when and how to code for scalability.
14
8. Don't optimize prematurely.
16
9. Don't pessimize prematurely.
18
10. Minimize global and shared data.
19
11. Hide information.
20
12. Know when and how to code for concurrency.

Ditto, they are just common sense.

Coding Style
27
14. Prefer compile- and link-time errors to run-time errors.

Who would discuss this? The whole problem is that sometimes it is
just not possible to do!
32
17. Avoid magic numbers.
34
18. Declare variables as locally as possible.

C99 allows you to define variables just besides their intended
usage so this applies to C too.

35
19. Always initialize variables.
36
20. Avoid long functions. Avoid deep nesting.
40
23. Make header files self-sufficient.
42
24. Always write internal #include guards. Never write external
#include guards.
Internal guards (if I understood correctly) would be this:

File win.h

#ifndef __win_h__
#define __win_h__
/* The rest of the win.h comes here */
#endif

External guards would be:
#ifndef __win_h__
#define __win_h__
#include <win.h>
#endif

This is OK, and I would agree with the rule, but I use sometimes

#ifndef __win_h__
#include <win.h>
#endif

This spares the preprocessor to open a file, and parse the whole
file...
I use more often the

#pragma once

that lcc-win32 provides to avoid all this guards stuff, and that
should be standardized as common practice. Microsoft uses the
same construct.

All in all an interesting message in this group. At last, a message
that tells something substantive

Apr 29 '06 #3
jacob navia schrieb:
Michael Mair a écrit :
ct***@163.com schrieb:
<snip: cn-URL advertising a translated version of "C++ Coding Standards"
and an excerpt of the list of rules>

While Herb and Andrei have done excellent work and the book
can serve as company C++ coding guideline or standard or
provide a good base for one, this has to do nothing whatsoever
with C. In fact, some of the more detailled rules do not make
sense or may even be harmful when applied to C.
Most of those rules apply to C too.


The general "short rule", yes, but what they are actually writing
as full rule, explanation and examples does not necessarily apply
to C. However, these are the _flesh_ of the respective rule.
The ones that are C++ specific are:
38 (initialization dependencies).
This is for C++ constructors stuff. Mostly irrelevant in C.
30 (Avoid macros).
In standard C is not possible to replace
#define MAX_SIZE 512
with
const int MAX_SIZE=512; <snip: lcc-win32 behaviour> 28 Use const proactively.
C++ "const" stuff is obscure and can be ignored in C. I
think this simplifies the language, at least it produces
less headaches to me :-)
I think this is a basic decision; you can still decide to
use const where necessary, wherever sensible or wherever possible
(e.g. apply const to all parameters to ensure that they are not
abused as auxiliary variables or stuff like that).
It makes sense to "const" early and consistently; especially when
dealing with third party libraries, you may run into problems.
21 (Ensure resources are owned by objects).
C is not object oriented, so this makes no sense, since
no destructors exists.

I am snipping the useless page numbers in the following.
The other rules are quite sensible
Organizational and Policy Issues
0. Don't sweat the small stuff. (Or: Know what not to standardize.)
1. Compile cleanly at high warning levels.
2. Use an automated build system.
3. Use a version control system.
4. Invest in code reviews.

All of those rules make sense in C, Python, Ada, Visual Basic...
They are just common sense.
Agreed.
Design Style
5. Give one entity one cohesive responsibility

I am not sure what this means in C, probably that you have one function
that organizes the software module calling the others, managing the
shared data, etc.

This is too abstract to be of any use.
No, this can make perfect sense depending on what you do.
In the simplest case, you make sure that one "basic" function
does exactly one thing without semantic side effects.
Example:
A function called sortEntries() should not change the "entries".

Whenever you build more complex functionality, you build it
from other functions. This goes quite nicely with some of the
following rules.
6. Correctness, simplicity, and clarity come first
7. Know when and how to code for scalability.
8. Don't optimize prematurely.
9. Don't pessimize prematurely.
As I do not have the book here at the moment, I can only go by
memory.
AFAIR, this is one of the cases I meant: What they are writing
there as examples has no relevance for C and may even pessimize
for some very basic compilers for embedded chips.
10. Minimize global and shared data.
11. Hide information.
12. Know when and how to code for concurrency.

Ditto, they are just common sense.

Coding Style
14. Prefer compile- and link-time errors to run-time errors.

Who would discuss this? The whole problem is that sometimes it is
just not possible to do!
There are enough people, especially among "Windows C/C++" crowd,
who are not aware of this and use run-time checks at each and
every corner -- and the programmes die ungracefully as the number
of checks makes sensible error handling virtually impossible.

As for what is possible: You can test some of the basic assumptions
(CHAR_BIT == 8, the used *intN* typedefs really map to 2s complement
exact width types with the required widths, ...) at compile time,
which many people don't do.
17. Avoid magic numbers.
18. Declare variables as locally as possible.

C99 allows you to define variables just besides their intended
usage so this applies to C too.
It even applies to C89 -- nobody hinders you to use a compound
statement; this can be interesting if you have conditionally
compiled code parts.
19. Always initialize variables.
20. Avoid long functions. Avoid deep nesting.
23. Make header files self-sufficient.
24. Always write internal #include guards. Never write external
#include guards.

Internal guards (if I understood correctly) would be this:

File win.h

#ifndef __win_h__
#define __win_h__
/* The rest of the win.h comes here */
#endif
Yes, but do not use leading underscores for your own macro
identifiers; I liked the H_HEADERNAME_H convention suggested by
someone in a recent thread.
External guards would be:
#ifndef __win_h__
#define __win_h__
#include <win.h>
#endif
You have understood that correctly.

This is OK, and I would agree with the rule, but I use sometimes

#ifndef __win_h__
#include <win.h>
#endif

This spares the preprocessor to open a file, and parse the whole
file...
Um, this is ugly: The header guards belong to the header _only_.
The implementor is free to change the header guard macro identifier
at will.
If you need something like that, then use a macro which is explicitly
defined only if you -- by whatever chain of includes -- have access
to the desired functionality, e.g.
#ifndef WHATEVER_INDICA TOR
# include "i_provide_what ever.h"
#endif

Relying on header guard names as WHATEVER_INDICA TOR is IMO dangerous
and unsafe and will often break with a new compiler, OS, library
version etc.

I am not talking about what an implementation does when providing
the standard library headers -- it is free to do whatever it likes
but if it breaks too easily, then this is a QOI matter.
I use more often the

#pragma once

that lcc-win32 provides to avoid all this guards stuff, and that
should be standardized as common practice. Microsoft uses the
same construct.


There are reasons against this; the first that comes to my mind:
Think of repeatedly included template files where you just set
macro parameters before including the template to generate the
desired code -- if the author of the including file has to change
the code to achieve the previous functionality just because
someone else included his file with #pragma once in yet another
include file included before that one, then you have broken the
code, maybe in a very subtle manner.
I wonder why you have not included all of the rules to prove
your point. I seem to remember that there were a good deal
more.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 29 '06 #4

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

Similar topics

5
3614
by: David Inada | last post by:
Looking for any info on coding standards for ASP? Any tools that help with the review/formatting of code? Is it done mainly for the jscript/vbscript components? David
3
2053
by: ganesan | last post by:
Hi Guys, Could any one knows the best coding standards styles(with variable declarations for c#) . and if any links or site with the best coding standards for .NET send me those links regards Ganesan
4
2291
by: dotNetDave | last post by:
About three weeks ago I released the first .NET coding standards book titled "VSDN Tips & Tricks .NET Coding Standards". Here is what the famous author/ speaker Deborah Kurata says about it: "David McCarter once again demonstrates his knack for pulling best practices into one cohesive unit with his new book "VSDN Tips and Tricks: .NET Coding Standards". This book includes everything from how to set up your project to how to declare...
5
5403
by: db2sysc | last post by:
ALl. Is it possible to get MS ACCESS CODING STANDARDS? TIA
144
6867
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this initiative. Typically I find that coding standards are written by some guy in the company who has a way of coding that he likes and then tries to force everybody else to write code the way he likes it, not for any rational reason, but simply for the...
7
2561
by: Ralph Lund | last post by:
Hi. I am starting a new project with C#. I am searching for "good" coding conventions. I know that there are some coding conventions from microsoft, (but they are very extensive and not clear). In the example programs of Microsoft they use different coding conventions: private members sometimes with underscore, sometimes without; when calling a method sometimes: method(param1, param2) or method ( param1, param2) (with or without...
7
4949
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already developed significant content for the C programming language that is available at: https://www.securecoding.cert.org/ by clicking on the "CERT C Programming Language Secure Coding Standard"
3
1776
by: editormt | last post by:
A recent poll asked if programming standards are used by development organisations... and if they are controlled. None: 20% Yes, but without control: 49% Yes, with control: 31% Participants: 369 Source: Methods & Tools (http://www.methodsandtools.com)
0
1672
by: pat | last post by:
CodeCheck Coding Standard's Support As a free service to our customers we offer support in developing "rule-files" for automating corporate coding standards. If you have a coding standard that you wish to automate please send the standard to us in PDF format so we can assist in developing the automation with codecheck. If you are developing codecheck rule-files and have a particular
9
2095
by: dom.k.black | last post by:
Can anyone recommend a good existing C++ coding standard - parctical, pragmatic and sensible? A company I joined recently are moving from C to C++, they are very much into coding standards. But I have a horibble feeling they might get a C programmer to write the C++ standard. Would be nice to be able to suggest an existing standard.
0
8738
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
8652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9086
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
8979
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
8939
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...
1
6575
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
5907
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4412
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...
3
2036
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.