gogogo
Syndetics cover image
Image from Syndetics

C++ coding standards : 101 rules, guidelines, and best practices / Herb Sutter; Andrei Alexandrescu.

By: Contributor(s): Material type: TextTextSeries: The C++ in-depth seriesPublication details: Boston : Addison-Wesley, 2007.Edition: 4. printingDescription: XV, 220 SISBN:
  • 9780321113580 (pbk.)
Other title:
  • C-plus-plus coding standards
Subject(s):
Contents:
C-plus-plus hundred-and-one
C-plus-plus-in-depth
Holdings
Item type Current library Call number Copy number Status Date due Barcode
Standard Loan Thurles Library Main Collection 005.133 SUT (Browse shelf(Opens below)) 1 Available R16465KRCT
Standard Loan Thurles Library Main Collection 005.133 SUT (Browse shelf(Opens below)) 1 Available R16468KRCT

Enhanced descriptions from Syndetics:

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.



Literaturverz. S. 187 - 193. - Literaturangaben.

C-plus-plus hundred-and-one

C-plus-plus-in-depth

Table of contents provided by Syndetics

  • Preface (p. xi)
  • Organizational and Policy Issues (p. 1)
  • 0 Don't sweat the small stuff. (Or: Know what not to standardize.) (p. 2)
  • 1 Compile cleanly at high warning levels (p. 4)
  • 2 Use an automated build system (p. 7)
  • 3 Use a version control system (p. 8)
  • 4 Invest in code reviews (p. 9)
  • Design Style (p. 11)
  • 5 Give one entity one cohesive responsibility (p. 12)
  • 6 Correctness, simplicity, and clarity come first (p. 13)
  • 7 Know when and how to code for scalability (p. 14)
  • 8 Don't optimize prematurely (p. 16)
  • 9 Don't pessimize prematurely (p. 18)
  • 10 Minimize global and shared data (p. 19)
  • 11 Hide information (p. 20)
  • 12 Know when and how to code for concurrency (p. 21)
  • 13 Ensure resources are owned by objects. Use explicit RAII and smart pointers (p. 24)
  • Coding Style (p. 27)
  • 14 Prefer compile- and link-time errors to run-time errors (p. 28)
  • 15 Use const proactively (p. 30)
  • 16 Avoid macros (p. 32)
  • 17 Avoid magic numbers (p. 34)
  • 18 Declare variables as locally as possible (p. 35)
  • 19 Always initialize variables (p. 36)
  • 20 Avoid long functions. Avoid deep nesting (p. 38)
  • 21 Avoid initialization dependencies across compilation units (p. 39)
  • 22 Minimize definitional dependencies. Avoid cyclic dependencies (p. 40)
  • 23 Make header files self-sufficient (p. 42)
  • 24 Always write internal #include guards. Never write external #include guards (p. 43)
  • Functions and Operators (p. 45)
  • 25 Take parameters appropriately by value, (smart) pointer, or reference (p. 46)
  • 26 Preserve natural semantics for overloaded operators (p. 47)
  • 27 Prefer the canonical forms of arithmetic and assignment operators (p. 48)
  • 28 Prefer the canonical form of ++ and --. Prefer calling the prefix forms (p. 50)
  • 29 Consider overloading to avoid implicit type conversions (p. 51)
  • 30 Avoid overloading &&, [double vertical line], or, (comma) (p. 52)
  • 31 Don't write code that depends on the order of evaluation of function arguments (p. 54)
  • Class Design and Inheritance (p. 55)
  • 32 Be clear what kind of class you're writing (p. 56)
  • 33 Prefer minimal classes to monolithic classes (p. 57)
  • 34 Prefer composition to inheritance (p. 58)
  • 35 Avoid inheriting from classes that were not designed to be base classes (p. 60)
  • 36 Prefer providing abstract interfaces (p. 62)
  • 37 Public inheritance is substitutability. Inherit, not to reuse, but to be reused (p. 64)
  • 38 Practice safe overriding (p. 66)
  • 39 Consider making virtual functions nonpublic, and public functions nonvirtual (p. 68)
  • 40 Avoid providing implicit conversions (p. 70)
  • 41 Make data members private, except in behaviorless aggregates (C-style structs) (p. 72)
  • 42 Don't give away your internals (p. 74)
  • 43 Pimpl judiciously (p. 76)
  • 44 Prefer writing nonmember nonfriend functions (p. 79)
  • 45 Always provide new and delete together (p. 80)
  • 46 If you provide any class-specific new, provide all of the standard forms (plain, in-place, and nothrow) (p. 82)
  • Construction, Destruction, and Copying (p. 85)
  • 47 Define and initialize member variables in the same order (p. 86)
  • 48 Prefer initialization to assignment in constructors (p. 87)
  • 49 Avoid calling virtual functions in constructors and destructors (p. 88)
  • 50 Make base class destructors public and virtual, or protected and nonvirtual (p. 90)
  • 51 Destructors, deallocation, and swap never fail (p. 92)
  • 52 Copy and destroy consistently (p. 94)
  • 53 Explicitly enable or disable copying (p. 95)
  • 54 Avoid slicing. Consider Clone instead of copying in base classes (p. 96)
  • 55 Prefer the canonical form of assignment (p. 99)
  • 56 Whenever it makes sense, provide a no-fail swap (and provide it correctly) (p. 100)
  • Namespaces and Modules (p. 103)
  • 57 Keep a type and its nonmember function interface in the same namespace (p. 104)
  • 58 Keep types and functions in separate namespaces unless they're specifically intended to work together (p. 106)
  • 59 Don't write namespace usings in a header file or before an #include (p. 108)
  • 60 Avoid allocating and deallocating memory in different modules (p. 111)
  • 61 Don't define entities with linkage in a header file (p. 112)
  • 62 Don't allow exceptions to propagate across module boundaries (p. 114)
  • 63 Use sufficiently portable types in a module's interface (p. 116)
  • Templates and Genericity (p. 119)
  • 64 Blend static and dynamic polymorphism judiciously (p. 120)
  • 65 Customize intentionally and explicitly (p. 122)
  • 66 Don't specialize function templates (p. 126)
  • 67 Don't write unintentionally nongeneric code (p. 128)
  • Error Handling and Exceptions (p. 129)
  • 68 Assert liberally to document internal assumptions and invariants (p. 130)
  • 69 Establish a rational error handling policy, and follow it strictly (p. 132)
  • 70 Distinguish between errors and non-errors (p. 134)
  • 71 Design and write error-safe code (p. 137)
  • 72 Prefer to use exceptions to report errors (p. 140)
  • 73 Throw by value, catch by reference (p. 144)
  • 74 Report, handle, and translate errors appropriately (p. 145)
  • 75 Avoid exception specifications (p. 146)
  • STL: Containers (p. 149)
  • 76 Use vector by default. Otherwise, choose an appropriate container (p. 150)
  • 77 Use vector and string instead of arrays (p. 152)
  • 78 Use vector (and string::c_str) to exchange data with non-C++ APIs (p. 153)
  • 79 Store only values and smart pointers in containers (p. 154)
  • 80 Prefer push_back to other ways of expanding a sequence (p. 155)
  • 81 Prefer range operations to single-element operations (p. 156)
  • 82 Use the accepted idioms to really shrink capacity and really erase elements (p. 157)
  • STL: Algorithms (p. 159)
  • 83 Use a checked STL implementation (p. 160)
  • 84 Prefer algorithm calls to handwritten loops (p. 162)
  • 85 Use the right STL search algorithm (p. 165)
  • 86 Use the right STL sort algorithm (p. 166)
  • 87 Make predicates pure functions (p. 168)
  • 88 Prefer function objects over functions as algorithm and comparer arguments (p. 170)
  • 89 Write function objects correctly (p. 172)
  • Type Safety (p. 173)
  • 90 Avoid type switching; prefer polymorphism (p. 174)
  • 91 Rely on types, not on representations (p. 176)
  • 92 Avoid using reinterpret_cast (p. 177)
  • 93 Avoid using static_cast on pointers (p. 178)
  • 94 Avoid casting away const (p. 179)
  • 95 Don't use C-style casts (p. 180)
  • 96 Don't memcpy or memcmp non-PODs (p. 182)
  • 97 Don't use unions to reinterpret representation (p. 183)
  • 98 Don't use varargs (ellipsis) (p. 184)
  • 99 Don't use invalid objects. Don't use unsafe functions (p. 185)
  • 100 Don't treat arrays polymorphically (p. 186)
  • Bibliography (p. 187)
  • Summary of Summaries (p. 195)
  • Index (p. 209)

Excerpt provided by Syndetics

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. Excerpted from C++ Coding Standards: 101 Rules, Guidelines, and Best Practices by Herb Sutter, Andrei Alexandrescu All rights reserved by the original copyright owners. Excerpts are provided for display purposes only and may not be reproduced, reprinted or distributed without the written permission of the publisher.

Author notes provided by Syndetics

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

Powered by Koha