C++ Coding Standards: 101 Rules, Guidelines, and Best Practices

4.19 avg rating
( 409 ratings by Goodreads )
 
9780321113580: C++ Coding Standards: 101 Rules, Guidelines, and Best Practices
View all copies of this ISBN edition:
 
 

Consistent, high-quality coding standards improve software quality, reduce time-to-market, promote teamwork, eliminate time wasted on inconsequential matters, and simplify maintenance. Now, two of the world's most respected C++ experts distill the rich collective experience of the global C++ community into a set of coding standards that every developer and development team can understand and use as a basis for their own coding standards.

The authors cover virtually every facet of C++ programming: design and coding style, functions, operators, class design, inheritance, construction/destruction, copying, assignment, namespaces, modules, templates, genericity, exceptions, STL containers and algorithms, and more. Each standard is described concisely, with practical examples. From type definition to error handling, this book presents C++ best practices, including some that have only recently been identified and standardized-techniques you may not know even if you've used C++ for years. Along the way, you'll find answers to questions like

  • What's worth standardizing--and what isn't?
  • What are the best ways to code for scalability?
  • What are the elements of a rational error handling policy?
  • How (and why) do you avoid unnecessary initialization, cyclic, and definitional dependencies?
  • When (and how) should you use static and dynamic polymorphism together?
  • How do you practice "safe" overriding?
  • When should you provide a no-fail swap?
  • Why and how should you prevent exceptions from propagating across module boundaries?
  • Why shouldn't you write namespace declarations or directives in a header file?
  • Why should you use STL vector and string instead of arrays?
  • How do you choose the right STL search or sort algorithm?
  • What rules should you follow to ensure type-safe code?

Whether you're working alone or with others, C++ Coding Standards will help you write cleaner code--and write it faster, with fewer hassles and less frustration.



"synopsis" may belong to another edition of this title.

About the Author:

Herb Sutter is the author of three highly acclaimed books, Exceptional C++ Style, Exceptional C++, and More Exceptional C++ (Addison-Wesley). He chairs the ISO C++ standards committee, and is contributing editor and columnist for C/C++ Users Journal. As a software architect for Microsoft, Sutter leads the design of C++ language extensions for .NET programming.

Andrei Alexandrescu is the author of the award-winning book Modern C++ Design (Addison-Wesley, 2001) and is a columnist for C/C++ Users Journal.



Excerpt. Reprinted by permission. All rights reserved.:

Get into a rut early: Do the same process the same way. Accumulate idioms. Standardize. The only difference(!) between Shakespeare and you was the size of his idiom list-not the size of his vocabulary.

--Alan Perlis emphasis ours

The best thing about standards is that there are so many to choose from.

--Variously attributed

We want to provide this book as a basis for your team's coding standards for two principal reasons:

A coding standard should reflect the community's best tried-and-true experience: It should contain proven idioms based on experience and solid understanding of the language. In particular, a coding standard should be based firmly on the extensive and rich software development literature, bringing together rules, guidelines, and best practices that would otherwise be left scattered throughout many sources.

Nature abhors a vacuum: If you don't consciously set out reasonable rules, usually someone else will try to push their own set of pet rules instead. A coding standard made that way usually has all of the least desirable properties of a coding standard; for example, many such standards try to enforce a minimalistic C style use of C++.

Many bad coding standards have been set by people who don't understand the language well, don't understand software development well, or try to legislate too much. A bad coding standard quickly loses credibility and at best even its valid guidelines are liable to be ignored by disenchanted programmers who dislike or disagree with its poorer guidelines. That's "at best"--at worst, a bad standard might actually be enforced.

How to Use This Book

Think. Do follow good guidelines conscientiously; but don't follow them blindly. In this book's Items, note the Exceptions clarifying the less common situations where the guidance may not apply. No set of guidelines, however good (and we think these ones are), should try to be a substitute for thinking.

Each development team is responsible for setting its own standards, and for setting them responsibly. That includes your team. If you are a team lead, involve your team members in setting the team's standards; people are more likely to follow standards they view as their own than they are to follow a bunch of rules they feel are being thrust upon them.

This book is designed to be used as a basis for, and to be included by reference in, your team's coding standards. It is not intended to be the Last Word in coding standards, because your team will have additional guidelines appropriate to your particular group or task, and you should feel free to add those to these Items. But we hope that this book will save you some of the work of (re)developing your own, by documenting and referencing widely-accepted and authoritative practices that apply nearly universally (with Exceptions as noted), and so help increase the quality and consistency of the coding standards you use.

Have your team read these guidelines with their rationales (i.e., the whole book, and selected Items' References to other books and papers as needed), and decide if there are any that your team simply can't live with (e.g., because of some situation unique to your project). Then commit to the rest. Once adopted, the team's coding standards should not be violated except after consulting with the whole team.

Finally, periodically review your guidelines as a team to include practical experience and feedback from real use.

Coding Standards and You

Good coding standards can offer many interrelated advantages:

  • Improved code quality: Encouraging developers to do the right things in a consistent way directly works to improve software quality and maintainability.
  • Improved development speed: Developers don't need to always make decisions starting from first principles.
  • Better teamwork: They help reduce needless debates on inconsequential issues and make it easier for teammates to read and maintain each other's code.
  • Uniformity in the right dimension: This frees developers to be creative in directions that matter.

Under stress and time pressure, people do what they've been trained to do. They fall back on habit. That's why ER units in hospitals employ experienced, trained personnel; even knowledgeable beginners would panic.

As software developers, we routinely face enormous pressure to deliver tomorrow's software yesterday. Under schedule pressure, we do what we are trained to do and are used to doing. Sloppy programmers who in normal times don't know good practices of software engineering (or aren't used to applying them) will write even sloppier and buggier code when pressure is on. Conversely, programmers who form good habits and practice them regularly will keep themselves organized and deliver quality code, fast.

The coding standards introduced by this book are a collection of guidelines for writing high-quality C++ code. They are the distilled conclusions of a rich collective experience of the C++ community. Much of this body of knowledge has only been available in bits and pieces spread throughout books, or as word-of-mouth wisdom. This book's intent is to collect that knowledge into a collection of rules that is terse, justified, and easy to understand and follow.

Of course, one can write bad code even with the best coding standards. The same is true of any language, process, or methodology. A good set of coding standards fosters good habits and discipline that transcend mere rules. That foundation, once acquired, opens the door to higher levels. There's no shortcut; you have to develop vocabulary and grammar before writing poetry. We just hope to make that easier.

We address this book to C++ programmers of all levels:

If you are an apprentice programmer, we hope you will find the rules and their rationale helpful in understanding what styles and idioms C++ supports most naturally. We provide a concise rationale and discussion for each rule and guideline to encourage you to rely on understanding, not just rote memorization.

For the intermediate or advanced programmer, we have worked hard to provide a detailed list of precise references for each rule. This way, you can do further research into the rule's roots in C++'s type system, grammar, and object model. At any rate, it is very likely that you work in a team on a complex project. Here is where coding standards really pay off-you can use them to bring the team to a common level and provide a basis for code reviews.

About This Book

We have set out the following design goals for this book:

Short is better than long: Huge coding standards tend to be ignored; short ones get read and used. Long Items tend to be skimmed; short ones get read and used.

Each Item must be noncontroversial: This book exists to document widely agreed upon standards, not to invent them. If a guideline is not appropriate in all cases, it will be presented that way (e.g., "Consider X..." instead of "Do X...") and we will note commonly accepted exceptions.

Each Item must be authoritative: The guidelines in this book are backed up by references to existing published works. This book is intended to also provide an index into the C++ literature.

Each Item must need saying: We chose not to define new guidelines for things that you'll do anyway, that are already enforced or detected by the compiler, or that are already covered under other Items.

Example: "Don't return a pointer/reference to an automatic variable" is a good guideline, but we chose not to include it in this book because all of the compilers we tried already emit a warning for this, and so the issue is already covered under the broader Item 1, "Compile cleanly at high warning levels."

Example: "Use an editor (or compiler, or debugger)" is a good guideline, but of course you'll use those tools anyway without being told; instead, we spend two of our first four Items on "Use an automated build system" and "Use a version control system."

Example: "Don't abuse goto" is a great Item, but in our experience programmers universally know this, and it doesn't need saying any more.

Each Item is laid out as follows:

  • Item title: The simplest meaningful sound bite we could come up with as a mnemonic for the rule.
  • Summary: The most essential points, briefly stated.
  • Discussion: An extended explanation of the guideline. This often includes brief rationale, but remember that the bulk of the rationale is intentionally left in the References.
  • Examples (if applicable): Examples that demonstrate a rule or make it memorable.
  • Exceptions (if applicable): Any (and usually rare) cases when a rule doesn't apply. But beware the trap of being too quick to think: "Oh, I'm special; this doesn't apply in my situation"-that rationalization is common, and commonly wrong.
  • References: See these parts of the C++ literature for the full details and analysis.

In each section, we chose to nominate a "most valuable Item." Often, it's the first Item in a section, because we tried to put important Items up front in each part; but other times an important Item couldn't be put up front, for flow or readability reasons, and we felt the need to call it out for special attention in this way.

"About this title" may belong to another edition of this title.

Other Popular Editions of the Same Title

9787115142054: C + + Coding Standards : 101 rules, guidelines and best practices

Featured Edition

ISBN 10:  711514205X ISBN 13:  9787115142054
Publisher: People Post Press, 1991
Softcover

Top Search Results from the AbeBooks Marketplace

1.

Alexandrescu, Andrei,Sutter, Herb
Published by Addison-Wesley Professional (2004)
ISBN 10: 0321113586 ISBN 13: 9780321113580
New Paperback Quantity Available: 3
Seller:
pickabook
(San francisco, CA, U.S.A.)

Book Description Addison-Wesley Professional, 2004. Paperback. Condition: New. Seller Inventory # mon0000202950

More information about this seller | Contact this seller

Buy New
US$ 44.45
Convert currency

Add to Basket

Shipping: US$ 3.99
Within U.S.A.
Destination, rates & speeds

2.

Sutter, Herb
Published by Addison-Wesley Professional (2004)
ISBN 10: 0321113586 ISBN 13: 9780321113580
New Paperback Quantity Available: 2
Seller:
Book Deals
(Lewiston, NY, U.S.A.)

Book Description Addison-Wesley Professional, 2004. Paperback. Condition: BRAND NEW. Seller Inventory # 0321113586_abe_bn

More information about this seller | Contact this seller

Buy New
US$ 59.98
Convert currency

Add to Basket

Shipping: FREE
Within U.S.A.
Destination, rates & speeds

3.

Herb Sutter
Published by Addison-Wesley Professional
ISBN 10: 0321113586 ISBN 13: 9780321113580
New Quantity Available: 1
Seller:
RushLtd
(Denver, CO, U.S.A.)

Book Description Addison-Wesley Professional. Condition: new. new. Seller Inventory # think0321113586

More information about this seller | Contact this seller

Buy New
US$ 60.00
Convert currency

Add to Basket

Shipping: FREE
Within U.S.A.
Destination, rates & speeds

4.

Herb Sutter, Andrei Alexandrescu
Published by Addison-Wesley Professional (2004)
ISBN 10: 0321113586 ISBN 13: 9780321113580
New Paperback Quantity Available: 1
Seller:
Ergodebooks
(RICHMOND, TX, U.S.A.)

Book Description Addison-Wesley Professional, 2004. Paperback. Condition: New. 1. Seller Inventory # DADAX0321113586

More information about this seller | Contact this seller

Buy New
US$ 61.40
Convert currency

Add to Basket

Shipping: US$ 3.99
Within U.S.A.
Destination, rates & speeds

5.

Sutter, Herb; Alexandrescu, Andrei
Published by Addison-Wesley Professional (2004)
ISBN 10: 0321113586 ISBN 13: 9780321113580
New Softcover Quantity Available: 1
Seller:
Irish Booksellers
(Portland, ME, U.S.A.)

Book Description Addison-Wesley Professional, 2004. Condition: New. book. Seller Inventory # M0321113586

More information about this seller | Contact this seller

Buy New
US$ 61.28
Convert currency

Add to Basket

Shipping: US$ 4.78
Within U.S.A.
Destination, rates & speeds

6.

Herb Sutter
ISBN 10: 0321113586 ISBN 13: 9780321113580
New Paperback Quantity Available: 1
Seller:
Grand Eagle Retail
(Wilmington, DE, U.S.A.)

Book Description Paperback. Condition: New. 1. Paperback. Consistent, high-quality coding standards improve software quality, reduce time-to-market, promote teamwork, eliminate time wasted on inconsequential matters, and simplify maintenance. Now.Shipping may be from multiple locations in the US or from the UK, depending on stock availability. 240 pages. 0.481. Seller Inventory # 9780321113580

More information about this seller | Contact this seller

Buy New
US$ 76.02
Convert currency

Add to Basket

Shipping: FREE
Within U.S.A.
Destination, rates & speeds

7.

Sutter, Herb; Alexandrescu, Andrei
Published by Addison-Wesley Professional
ISBN 10: 0321113586 ISBN 13: 9780321113580
New PAPERBACK Quantity Available: 1
Seller:
Russell Books
(Victoria, BC, Canada)

Book Description Addison-Wesley Professional. PAPERBACK. Condition: New. 0321113586 Special order direct from the distributor. Seller Inventory # ING9780321113580

More information about this seller | Contact this seller

Buy New
US$ 80.99
Convert currency

Add to Basket

Shipping: US$ 6.99
From Canada to U.S.A.
Destination, rates & speeds

8.

Sutter, Herb; Alexandrescu, Andrei
ISBN 10: 0321113586 ISBN 13: 9780321113580
New Quantity Available: 1
Seller:
BennettBooksLtd
(San Diego, CA, U.S.A.)

Book Description Condition: New. New. Seller Inventory # S-0321113586

More information about this seller | Contact this seller

Buy New
US$ 97.75
Convert currency

Add to Basket

Shipping: US$ 4.95
Within U.S.A.
Destination, rates & speeds

9.

Herb Sutter
Published by Pearson Education (US)
ISBN 10: 0321113586 ISBN 13: 9780321113580
New Paperback / softback Quantity Available: 1
Seller:
THE SAINT BOOKSTORE
(Southport, United Kingdom)

Book Description Pearson Education (US). Paperback / softback. Condition: New. New copy - Usually dispatched within 2 working days. Seller Inventory # B9780321113580

More information about this seller | Contact this seller

Buy New
US$ 92.54
Convert currency

Add to Basket

Shipping: US$ 13.57
From United Kingdom to U.S.A.
Destination, rates & speeds

10.

Herb Sutter/ Andrei Alexandrescu
Published by Addison-Wesley (2004)
ISBN 10: 0321113586 ISBN 13: 9780321113580
New Paperback Quantity Available: 1
Seller:
Revaluation Books
(Exeter, United Kingdom)

Book Description Addison-Wesley, 2004. Paperback. Condition: Brand New. 1st edition. 240 pages. 9.00x7.25x0.75 inches. In Stock. Seller Inventory # x-0321113586

More information about this seller | Contact this seller

Buy New
US$ 84.05
Convert currency

Add to Basket

Shipping: US$ 37.18
From United Kingdom to U.S.A.
Destination, rates & speeds

There are more copies of this book

View all search results for this book