did-you-know? rent-now

Amazon no longer offers textbook rentals. We do!

did-you-know? rent-now

Amazon no longer offers textbook rentals. We do!

We're the #1 textbook rental company. Let us show you why.

9780321113580

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

by ; ;
  • ISBN13:

    9780321113580

  • ISBN10:

    0321113586

  • Edition: 1st
  • Format: Paperback
  • Copyright: 2004-10-25
  • Publisher: Addison-Wesley Professional
  • Purchase Benefits
  • Free Shipping Icon Free Shipping On Orders Over $35!
    Your order must be $35 or more to qualify for free economy shipping. Bulk sales, PO's, Marketplace items, eBooks and apparel do not qualify for this offer.
  • eCampus.com Logo Get Rewarded for Ordering Your Textbooks! Enroll Now
List Price: $59.99 Save up to $6.00
  • Digital
    $53.99
    Add to Cart

    DURATION
    PRICE

Supplemental Materials

What is included with this book?

Summary

Essential guidelines for writing high-quality C++ code - taught by two foremost masters in the field.

Author Biography

Herb Sutter is 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 a columnist for C/C++ Users Journal

Table of Contents

Prefacep. xi
Organizational and Policy Issuesp. 1
Don't sweat the small stuff. (Or: Know what not to standardize.)p. 2
Compile cleanly at high warning levelsp. 4
Use an automated build systemp. 7
Use a version control systemp. 8
Invest in code reviewsp. 9
Design Stylep. 11
Give one entity one cohesive responsibilityp. 12
Correctness, simplicity, and clarity come firstp. 13
Know when and how to code for scalabilityp. 14
Don't optimize prematurelyp. 16
Don't pessimize prematurelyp. 18
Minimize global and shared datap. 19
Hide informationp. 20
Know when and how to code for concurrencyp. 21
Ensure resources are owned by objects. Use explicit RAII and smart pointersp. 24
Coding Stylep. 27
Prefer compile- and link-time errors to run-time errorsp. 28
Use const proactivelyp. 30
Avoid macrosp. 32
Avoid magic numbersp. 34
Declare variables as locally as possiblep. 35
Always initialize variablesp. 36
Avoid long functions. Avoid deep nestingp. 38
Avoid initialization dependencies across compilation unitsp. 39
Minimize definitional dependencies. Avoid cyclic dependenciesp. 40
Make header files self-sufficientp. 42
Always write internal #include guards. Never write external #include guardsp. 43
Functions and Operatorsp. 45
Take parameters appropriately by value, (smart) pointer, or referencep. 46
Preserve natural semantics for overloaded operatorsp. 47
Prefer the canonical forms of arithmetic and assignment operatorsp. 48
Prefer the canonical form of ++ and --. Prefer calling the prefix formsp. 50
Consider overloading to avoid implicit type conversionsp. 51
Avoid overloading &&, [double vertical line], or, (comma)p. 52
Don't write code that depends on the order of evaluation of function argumentsp. 54
Class Design and Inheritancep. 55
Be clear what kind of class you're writingp. 56
Prefer minimal classes to monolithic classesp. 57
Prefer composition to inheritancep. 58
Avoid inheriting from classes that were not designed to be base classesp. 60
Prefer providing abstract interfacesp. 62
Public inheritance is substitutability. Inherit, not to reuse, but to be reusedp. 64
Practice safe overridingp. 66
Consider making virtual functions nonpublic, and public functions nonvirtualp. 68
Avoid providing implicit conversionsp. 70
Make data members private, except in behaviorless aggregates (C-style structs)p. 72
Don't give away your internalsp. 74
Pimpl judiciouslyp. 76
Prefer writing nonmember nonfriend functionsp. 79
Always provide new and delete togetherp. 80
If you provide any class-specific new, provide all of the standard forms (plain, in-place, and nothrow)p. 82
Construction, Destruction, and Copyingp. 85
Define and initialize member variables in the same orderp. 86
Prefer initialization to assignment in constructorsp. 87
Avoid calling virtual functions in constructors and destructorsp. 88
Make base class destructors public and virtual, or protected and nonvirtualp. 90
Destructors, deallocation, and swap never failp. 92
Copy and destroy consistentlyp. 94
Explicitly enable or disable copyingp. 95
Avoid slicing. Consider Clone instead of copying in base classesp. 96
Prefer the canonical form of assignmentp. 99
Whenever it makes sense, provide a no-fail swap (and provide it correctly)p. 100
Namespaces and Modulesp. 103
Keep a type and its nonmember function interface in the same namespacep. 104
Keep types and functions in separate namespaces unless they're specifically intended to work togetherp. 106
Don't write namespace usings in a header file or before an #includep. 108
Avoid allocating and deallocating memory in different modulesp. 111
Don't define entities with linkage in a header filep. 112
Don't allow exceptions to propagate across module boundariesp. 114
Use sufficiently portable types in a module's interfacep. 116
Templates and Genericityp. 119
Blend static and dynamic polymorphism judiciouslyp. 120
Customize intentionally and explicitlyp. 122
Don't specialize function templatesp. 126
Don't write unintentionally nongeneric codep. 128
Error Handling and Exceptionsp. 129
Assert liberally to document internal assumptions and invariantsp. 130
Establish a rational error handling policy, and follow it strictlyp. 132
Distinguish between errors and non-errorsp. 134
Design and write error-safe codep. 137
Prefer to use exceptions to report errorsp. 140
Throw by value, catch by referencep. 144
Report, handle, and translate errors appropriatelyp. 145
Avoid exception specificationsp. 146
STL: Containersp. 149
Use vector by default. Otherwise, choose an appropriate containerp. 150
Use vector and string instead of arraysp. 152
Use vector (and string::c_str) to exchange data with non-C++ APIsp. 153
Store only values and smart pointers in containersp. 154
Prefer push_back to other ways of expanding a sequencep. 155
Prefer range operations to single-element operationsp. 156
Use the accepted idioms to really shrink capacity and really erase elementsp. 157
STL: Algorithmsp. 159
Use a checked STL implementationp. 160
Prefer algorithm calls to handwritten loopsp. 162
Use the right STL search algorithmp. 165
Use the right STL sort algorithmp. 166
Make predicates pure functionsp. 168
Prefer function objects over functions as algorithm and comparer argumentsp. 170
Write function objects correctlyp. 172
Type Safetyp. 173
Avoid type switching; prefer polymorphismp. 174
Rely on types, not on representationsp. 176
Avoid using reinterpret_castp. 177
Avoid using static_cast on pointersp. 178
Avoid casting away constp. 179
Don't use C-style castsp. 180
Don't memcpy or memcmp non-PODsp. 182
Don't use unions to reinterpret representationp. 183
Don't use varargs (ellipsis)p. 184
Don't use invalid objects. Don't use unsafe functionsp. 185
Don't treat arrays polymorphicallyp. 186
Bibliographyp. 187
Summary of Summariesp. 195
Indexp. 209
Table of Contents provided by Rittenhouse. All Rights Reserved.

Supplemental Materials

What is included with this book?

The New copy of this book will include any supplemental materials advertised. Please check the title of the book to determine if it should include any access cards, study guides, lab manuals, CDs, etc.

The Used, Rental and eBook copies of this book are not guaranteed to include any supplemental materials. Typically, only the book itself is included. This is true even if the title states it includes any access cards, study guides, lab manuals, CDs, etc.

Excerpts

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 red

Rewards Program