Clean C++20 (eBook)
XVII, 499 Seiten
Apress (Verlag)
978-1-4842-5949-8 (ISBN)
- Gain sound principles and rules for clean coding in C++
- Carry out test-driven development (TDD)
- Better modularize your C++ code base
- Discover and apply C++ design patterns and idioms
- Write C++ code in both object-oriented and functional programming styles
Table of Contents 5
About the Author 12
About the Technical Reviewer 13
Acknowledgments 14
Chapter 1: Introduction 15
Software Entropy 17
Why C++? 18
Clean Code 20
C++11: The Beginning of a New Era 20
Who This Book Is For 21
Conventions Used in This Book 22
Sidebars 23
Notes, Tips, and Warnings 23
Code Samples 23
Coding Style 24
C++ Core Guidelines 25
Companion Website and Source Code Repository 25
UML Diagrams 26
Chapter 2: Build a Safety Net 27
The Need for Testing 27
Introduction to Testing 30
Unit Tests 33
What About QA? 35
Rules for Good Unit Tests 36
Test Code Quality 36
Unit Test Naming 36
Unit Test Independence 38
One Assertion per Test 39
Independent Initialization of Unit Test Environments 40
Exclude Getters and Setters 41
Exclude Third-Party Code 41
Exclude External Systems 42
What Do We Do with the Database? 42
Don’t Mix Test Code with Production Code 43
Tests Must Run Fast 46
How Do You Find a Test’s Input Data? 47
Equivalence Partitioning 47
Boundary Value Analysis 49
Test Doubles (Fake Objects) 50
Chapter 3: Be Principled 54
What Is a Principle? 54
KISS 55
YAGNI 56
DRY 57
It’s About Knowledge! 57
Building Abstractions Is Sometimes Hard 58
Information Hiding 61
Strong Cohesion 66
Loose Coupling 69
Be Careful with Optimizations 73
Principle of Least Astonishment (PLA) 74
The Boy Scout Rule 75
Collective Code Ownership 76
Chapter 4: Basics of Clean C++ 77
Good Names 78
Names Should Be Self-Explanatory 80
Use Names from the Domain 82
Choose Names at an Appropriate Level of Abstraction 84
Avoid Redundancy When Choosing a Name 85
Avoid Cryptic Abbreviations 86
Avoid Hungarian Notation and Prefixes 87
Avoid Using the Same Name for Different Purposes 88
Comments 89
Let the Code Tell the Story 89
Do Not Comment Obvious Things 90
Don’t Disable Code with Comments 91
Don’t Write Block Comments 92
Don’t Use Comments to Substitute Version Control 96
The Rare Cases Where Comments Are Useful 96
Documentation Generation from Source Code 98
Functions 101
One Thing, No More! 105
Let Them Be Small 106
“But the Call Time Overhead!” 107
Function Naming 108
Use Intention-Revealing Names 109
Parameters and Return Values 110
Number of Parameters 110
Avoid Flag Parameters 112
Avoid Output Parameters 115
Don’t Pass or Return 0 (NULL, nullptr) 116
Strategies for Avoiding Regular Pointers 119
Choose simple object construction on the stack instead of on the heap 119
In a function’s argument list, use (const) references instead of pointers 122
If it is inevitable to deal with a pointer to a resource, use a smart one 123
If an API returns a raw pointer... 123
The Power of const Correctness 124
About Old C-Style in C++ Projects 126
Choose C++ Strings and Streams over Old C-Style char* 127
Avoid Using printf(), sprintf(), gets(), etc. 129
Choose Standard Library Containers over Simple C-Style Arrays 134
Use C++ Casts Instead of Old C-Style Casts 137
Avoid Macros 140
Chapter 5: Advanced Concepts of Modern C++ 143
Managing Resources 144
Resource Acquisition Is Initialization (RAII) 146
Smart Pointers 147
Unique Ownership with std::unique_ptr< T>
Shared Ownership with std::shared_ptr< T>
No Ownership, but Secure Access with std::weak_ptr< T>
Atomic Smart Pointers 155
Avoid Explicit New and Delete 156
Managing Proprietary Resources 156
We Like to Move It 158
What Are Move Semantics? 158
The Matter with Those lvalues and rvalues 160
rvalue References 162
Don’t Enforce Move Everywhere 164
The Rule of Zero 165
The Compiler Is Your Colleague 171
Automatic Type Deduction 171
Computations During Compile Time 176
Variable Templates 179
Don’t Allow Undefined Behavior 181
Type-Rich Programming 183
Know Your Libraries 194
Take Advantage of < algorithm>
Easier Parallelization of Algorithms Since C++17 197
Sorting and Output of a Container 200
More Convenience with Ranges 201
Non-Owning Ranges with Views 202
Comparing Two Sequences 203
Take Advantage of Boost 206
More Libraries That You Should Know About 206
Proper Exception and Error Handling 208
Prevention Is Better Than Aftercare 209
No Exception Safety 210
Basic Exception Safety 210
Strong Exception Safety 211
The No-Throw Guarantee 212
An Exception Is an Exception, Literally! 214
If You Can’t Recover, Get Out Quickly 216
Define User-Specific Exception Types 217
Throw by Value, Catch by const Reference 219
Pay Attention to the Correct Order of Catch Clauses 220
Interface Design 221
Attributes 222
noreturn (since C++11) 223
deprecated (since C++14) 223
nodiscard (since C++17) 224
maybe_unused (since C++17) 225
Concepts: Requirements for Template Arguments 227
Specifying a Concept 229
Applying a Concept 229
Chapter 6: Modularization 232
The Basics of Modularization 233
Criteria for Finding Modules 233
Focus on the Domain of Your Software 234
Abstraction 235
Choose a Hierarchical Decomposition 235
Single Responsibility Principle (SRP) 236
Single Level of Abstraction (SLA) 237
The Whole Enchilada 238
Object-Orientation 238
Object-Oriented Thinking 239
Principles for Good Class Design 241
Keep Classes Small 241
Open-Closed Principle (OCP) 243
A Short Comparison of Type Erasure Techniques 244
Liskov Substitution Principle (LSP) 251
The Square-Rectangle Dilemma 252
Favor Composition over Inheritance 264
Interface Segregation Principle (ISP) 266
Acyclic Dependency Principle 269
Dependency Inversion Principle (DIP) 273
Don’t Talk to Strangers (The Law of Demeter) 280
Avoid Anemic Classes 286
Tell, Don’t Ask! 287
Avoid Static Class Members 290
Modules 292
The Drawbacks of #include 292
Modules to the Rescue 294
Under the Hood 295
Three Options for Using Modules 297
Include Translation 297
Header Importation 298
Module Importation 299
Separating Interface and Implementation 300
The Impact of Modules 301
Chapter 7: Functional Programming 303
What Is Functional Programming? 305
What Is a Function? 306
Pure vs Impure Functions 307
Functional Programming in Modern C++ 309
Functional Programming with C++ Templates 309
Function-Like Objects (Functors) 312
Generator 313
Unary Function 316
Predicate 318
Binary Functors 321
Binders and Function Wrappers 322
Lambda Expressions 325
Generic Lambda Expressions (C++14) 328
Lambda Templates (C++20) 329
Higher-Order Functions 332
Map, Filter, and Reduce 334
Map 334
Filter 334
Reduce (Fold) 335
Fold Expressions in C++17 337
Pipelining with Range Adaptors (C++20) 339
Clean Code in Functional Programming 343
Chapter 8: Test-Driven Development 345
The Drawbacks of Plain Old Unit Testing (POUT) 346
Test-Driven Development as a Game Changer 348
The Workflow of TDD 349
TDD by Example: The Roman Numerals Code Kata 352
Preparations 353
The First Test 356
The Second Test 359
The Third Test and the Tidying Afterward 359
More Sophisticated Tests with a Custom Assertion 364
It’s Time to Clean Up Again 369
Approaching the Finish Line 372
Done! 374
The Advantages of TDD 377
When We Should Not Use TDD 379
TDD Is Not a Replacement for Code Reviews 381
Chapter 9: Design Patterns and Idioms 384
Design Principles vs Design Patterns 385
Some Patterns and When to Use Them 386
Dependency Injection (DI) 387
The Singleton Anti-Pattern 387
Dependency Injection to the Rescue 392
Adapter 403
Strategy 405
Command 411
Command Processor 416
Composite 421
Observer 425
Factories 431
Simple Factory 431
Facade 434
The Money Class 436
Special Case Object (Null Object) 440
What Is an Idiom? 444
Some Useful C++ Idioms 445
The Power of Immutability 445
Substitution Failure Is Not an Error (SFINAE) 448
The Copy-and-Swap Idiom 452
Pointer to Implementation (PIMPL) 456
Appendix A:Small UML Guide 460
Structural Modeling 461
Component 461
Class and Object 462
Interface 466
Association 468
Generalization 471
Dependency 472
Template and Template Binding 474
Behavioral Modeling 475
Activity Diagram 475
Action 476
Control Flow Edge 477
Other Activity Nodes 477
Sequence Diagram 478
Lifeline 479
Message 480
State Diagram 480
State 481
Transitions 482
External Transitions 482
Internal Transitions 482
Trigger 483
Stereotypes 483
Bibliography 485
Index 489
Erscheint lt. Verlag | 17.6.2021 |
---|---|
Zusatzinfo | XVII, 491 p. 80 illus., 7 illus. in color. |
Sprache | englisch |
Themenwelt | Mathematik / Informatik ► Informatik ► Programmiersprachen / -werkzeuge |
Mathematik / Informatik ► Informatik ► Software Entwicklung | |
Informatik ► Theorie / Studium ► Compilerbau | |
Schlagworte | Applications • Best Practices • C++ • C++20 • clean • Code • Development • programming • Software • source |
ISBN-10 | 1-4842-5949-1 / 1484259491 |
ISBN-13 | 978-1-4842-5949-8 / 9781484259498 |
Informationen gemäß Produktsicherheitsverordnung (GPSR) | |
Haben Sie eine Frage zum Produkt? |
Größe: 7,3 MB
DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasserzeichen und ist damit für Sie personalisiert. Bei einer missbräuchlichen Weitergabe des eBooks an Dritte ist eine Rückverfolgung an die Quelle möglich.
Dateiformat: PDF (Portable Document Format)
Mit einem festen Seitenlayout eignet sich die PDF besonders für Fachbücher mit Spalten, Tabellen und Abbildungen. Eine PDF kann auf fast allen Geräten angezeigt werden, ist aber für kleine Displays (Smartphone, eReader) nur eingeschränkt geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.
Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich