Um unsere Webseiten für Sie optimal zu gestalten und fortlaufend zu verbessern, verwenden wir Cookies. Durch Bestätigen des Buttons »Akzeptieren« stimmen Sie der Verwendung zu. Über den Button »Einstellungen« können Sie auswählen, welche Cookies Sie zulassen wollen.

AkzeptierenEinstellungen
Nicht aus der Schweiz? Besuchen Sie lehmanns.de
Clean C++20 -  Stephan Roth

Clean C++20 (eBook)

Sustainable Software Development Patterns and Best Practices

(Autor)

eBook Download: PDF
2021 | 2. Auflage
XVII, 499 Seiten
Apress (Verlag)
978-1-4842-5949-8 (ISBN)
Systemvoraussetzungen
46,99 inkl. MwSt
(CHF 45,90)
Der eBook-Verkauf erfolgt durch die Lehmanns Media GmbH (Berlin) zum Preis in Euro inkl. MwSt.
  • Download sofort lieferbar
  • Zahlungsarten anzeigen
Write maintainable, extensible, and durable software with modern C++. This book, updated for the recently released C++20 standard, is a must for every developer, software architect, or team leader who is interested in well-crafted C++ code, and thus also wants to save development costs. If you want to teach yourself about writing better C++ code, Clean C++20 is exactly what you need. It is written for C++ developers of all skill levels and shows by example how to write understandable, flexible, maintainable, and efficient C++ code. Even if you are a seasoned C++ developer, there are nuggets and data points in this book that you will find useful in your work.

If you don't take care with your codebase, you can produce a large, messy, and unmaintainable beast in any programming language. However, C++ projects in particular are prone to get messy and tend to slip into a maintenance nightmare. There is lots of C++ code out there that looks as if it was written in the 1980s, completely ignoring principles and practices of well-written and modern C++.

It seems that C++ developers have been forgotten by those who preach Software Craftsmanship and Clean Code principles. The web is full of C++ code examples that may be very fast and highly optimized, but whose developers have completely ignored elementary principles of good design and well-written code. This book will explain how to avoid this and how to get the most out of your C++ code. You'll find your coding becomes more efficient and, importantly, more fun.

What You Will Learn
  • 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
Who This Book Is For

Any C++ developer or software engineer with an interest in producing better code. 


Stephan Roth is a coach, consultant, and trainer for systems and software engineering with German consultancy company oose Innovative Informatik eG located in Hamburg. Before he joined oose, he worked for many years as a software developer, software architect, and systems engineer in the field of radio reconnaissance and communication intelligence systems. He has developed sophisticated applications, especially in a high-performance system environment, and graphical user interfaces using C++ and other programming languages. Stephan is an active supporter of the Software Craftsmanship movement and is concerned with principles and practices of Clean Code Development (CCD).

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?
PDFPDF (Wasserzeichen)
Größe: 7,3 MB

DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasser­zeichen und ist damit für Sie persona­lisiert. Bei einer missbräuch­lichen Weiter­gabe des eBooks an Dritte ist eine Rück­ver­folgung an die Quelle möglich.

Dateiformat: PDF (Portable Document Format)
Mit einem festen Seiten­layout eignet sich die PDF besonders für Fach­bücher mit Spalten, Tabellen und Abbild­ungen. Eine PDF kann auf fast allen Geräten ange­zeigt werden, ist aber für kleine Displays (Smart­phone, eReader) nur einge­schrä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.

Mehr entdecken
aus dem Bereich
For Better Code, Performance, and Scalability

von Kevin Gosse; Konrad Kokosa; Christophe Nasarre

eBook Download (2024)
Apress (Verlag)
CHF 107,45
Hash Table Programming with C

von Thomas Mailund

eBook Download (2024)
Apress (Verlag)
CHF 53,70