Clean C++ - Stephan Roth

Clean C++ (eBook)

Sustainable Software Development Patterns and Best Practices with C++ 17

(Autor)

eBook Download: PDF
2017 | 1st ed.
XVII, 291 Seiten
Apress (Verlag)
978-1-4842-2793-0 (ISBN)
Systemvoraussetzungen
56,99 inkl. MwSt
  • Download sofort lieferbar
  • Zahlungsarten anzeigen
Write maintainable, extensible, and durable software with modern C++. This book is a must for every developer, software architect, or team leader who is interested in good C++ code, and thus also wants to save development costs. If you want to teach yourself about writing clean C++, Clean C++ is exactly what you need. It is written to help 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 code, you can produce a large, messy, and unmaintainable beast in any programming language. However, C++ projects in particular are prone to be messy and tend to slip into bad habits. Lots of C++ code that is written today looks as if it was written in the 1980s.

It seems that C++ developers have been forg
otten by those who preach Software Craftsmanship and Clean Code principles. The Web is full of bad, but apparently very fast and highly optimized C++ code examples, with cruel syntax that completely ignores elementary principles of good design and well-written code. This book will explain how to avoid this scenario 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'll Learn
  • Gain sound principles and rules for clean coding in C++
  • Carry out test driven development (TDD)
  • Discover C++ design patterns and idioms
  • Apply these design patterns

Who This Book Is For

Any C++ developer and 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).


Write maintainable, extensible, and durable software with modern C++. This book is a must for every developer, software architect, or team leader who is interested in good C++ code, and thus also wants to save development costs. If you want to teach yourself about writing clean C++, Clean C++ is exactly what you need. It is written to help 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 code, you can produce a large, messy, and unmaintainable beast in any programming language. However, C++ projects in particular are prone to be messy and tend to slip into bad habits. Lots of C++ code that is written today looks as if it was written in the 1980s.It seems that C++ developers have been forgotten by those who preach Software Craftsmanship and Clean Code principles. The Web is full of bad, but apparently very fast and highly optimized C++ code examples, with cruel syntax that completely ignores elementary principles of good design and well-written code. This book will explain how to avoid this scenario 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'll LearnGain sound principles and rules for clean coding in C++Carry out test driven development (TDD)Discover C++ design patterns and idiomsApply these design patternsWho This Book Is ForAny C++ developer and 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).

Contents at a Glance 5
Contents 6
About the Author 12
About the Technical Reviewer 13
Acknowledgments 14
Chapter 1: Introduction 15
Software Entropy 16
Clean Code 17
Why C++? 18
C++11 – The Beginning of a New Era 18
Who This Book Is For 19
Conventions Used in This Book 19
Sidebars 19
Notes, Tips, and Warnings 20
Code Samples 20
Coding Style 20
Companion Website and Source Code Repository 21
UML Diagrams 21
Chapter 2: Build a Safety Net 22
The Need for Testing 22
Introduction into Testing 24
Unit Tests 26
What about QA? 27
Rules for Good Unit Tests 28
Test Code Quality 28
Unit Test Naming 28
Unit Test Independence 29
One Assertion per Test 30
Independent Initialization of Unit Test Environments 31
Exclude Getters and Setters 31
Exclude Third-Party Code 31
Exclude External Systems 32
And What Do We Do with the Database? 32
Don’t Mix Test Code with Production Code 32
Tests Must Run Fast 35
Test Doubles (Fake Objects) 35
Chapter 3: Be Principled 39
What Is a Principle? 39
KISS 40
YAGNI 40
DRY 41
Information Hiding 41
Strong Cohesion 44
Loose Coupling 47
Be Careful with Optimizations 51
Principle of Least Astonishment (PLA) 51
The Boy Scout Rule 51
Chapter 4: Basics of Clean C++ 53
Good Names 53
Names Should Be Self-Explanatory 55
Use Names from the Domain 56
Choose Names at an Appropriate Level of Abstraction 57
Avoid Redundancy When Choosing a Name 58
Avoid Cryptic Abbreviations 58
Avoid Hungarian Notation and Prefixes 59
Avoid Using the Same Name for Different Purposes 60
Comments 60
Let the Code Tell a Story 61
Do Not Comment Obvious Things 61
Don’t Disable Code with Comments 62
Don’t Write Block Comments 62
Don’t Use Comments to Substitute Version Control 64
The Rare Cases Where Comments Are Useful 65
Documentation Generation from Source Code 66
Functions 68
One Thing, No More! 70
Let Them Be Small 71
“But the Call Time Overhead!” 71
Function Naming 72
Use Intention-Revealing Names 73
Arguments and Return Values 73
Number of Arguments 74
Avoid Flag Arguments 75
Avoid Output Arguments 77
Don’t Pass or Return 0 (NULL, nullptr) 79
Strategies to Avoid Regular Pointers 80
Prefer simple object construction on the stack instead of on the heap 80
In a function’s argument list, use (const) references instead of pointers 82
If it is inevitable to deal with a pointer to a resource, use a smart one 82
If an API returns a raw pointer… 82
The Power of const correctness 82
About Old C-style in C++ Projects 84
Prefer C++ Strings and Streams over Old C-Style char* 84
Avoid Using printf(), sprintf(), gets(), etc. 86
Prefer Standard Library Containers over Simple C-style Arrays 89
Use C++ casts Instead of Old C-Style Casts 92
Avoid Macros 94
Chapter 5: Advanced Concepts of Modern C++ 96
Managing Resources 96
Resource Acquisition Is Initialization (RAII) 98
Smart Pointers 98
Unique Ownership with std::unique_ptr< T>
Shared Ownership with std::shared_ptr< T>
No Ownership, but Secure Access with std::weak_ptr< T>
Avoid Explicit New and Delete 104
Managing Proprietary Resources 104
We Like to Move It 105
What Are Move Semantics? 106
The Matter with Those lvalues and rvalues 107
rvalue References 108
Don’t Enforce Move Everywhere 109
The Rule of Zero 110
The Compiler Is Your Colleague 113
Automatic Type Deduction 114
Computations during Compile Time 117
Variable Templates 119
Don’t Allow Undefined Behavior 120
Type-Rich Programming 121
Know Your Libraries 127
Take Advantage of < algorithm>
Easier Parallelization of Algorithms Since C++17 129
Sorting and Output of a Container 131
Comparing Two Sequences 132
Take Advantage of Boost 133
More Libraries That You Should Know About 134
Proper Exception and Error Handling 134
Prevention Is Better Than Aftercare 135
No Exception-Safety 136
Basic Exception-Safety 136
Strong Exception-Safety 136
The No-Throw Guarantee 137
An Exception Is an Exception – Literally! 139
If You Can’t Recover, Get Out Quickly 140
Define User-Specific Exception Types 140
Throw by Value, Catch by const Reference 142
Pay Attention to the Correct Order of Catch-Clauses 142
Chapter 6: Object Orientation 144
Object-Oriented Thinking 144
Abstraction – the Key to Master Complexity 146
Principles for Good Class Design 146
Keep Classes Small 147
Single Responsibility Principle (SRP) 148
Open-Closed Principle (OCP) 148
Liskov Substitution Principle (LSP) 149
The Square-Rectangle Dilemma 149
Favor Composition over Inheritance 158
Interface Segregation Principle (ISP) 160
Acyclic Dependency Principle 161
Dependency Inversion Principle (DIP) 164
Don’t Talk to Strangers (Law of Demeter) 169
Avoid Anemic Classes 174
Tell, Don’t Ask! 174
Avoid Static Class Members 176
Chapter 7: Functional Programming 178
What Is Functional Programming? 179
What Is a Function? 180
Pure vs. Impure Functions 181
Functional Programming in Modern C++ 182
Functional Programming with C++ Templates 182
Function-Like Objects (Functors) 184
Generator 184
Unary Function 187
Predicates 187
Binary Functors 190
Binders and Function Wrappers 190
Lambda Expressions 192
Generic Lambda Expressions (C++14) 194
Higher-Order Functions 195
Map, Filter, and Reduce 196
Map 197
Filter 197
Reduce (Fold) 198
Fold Expressions in C++17 199
Clean Code in Functional Programming 200
Chapter 8: Test-Driven Development 202
The Drawbacks of Plain Old Unit Testing (POUT) 203
Test-Driven Development as a Game Changer 204
The Workflow of TDD 204
TDD by Example: The Roman Numerals Code Kata 207
Preparations 208
The First Test 210
The Second Test 212
The Third Test and the Tidying Afterwards 212
More Sophisticated Tests with a Custom Assertion 216
It’s Time to Clean Up Again 218
Approaching the Finish Line 221
Done! 222
The Advantages of TDD 224
When We Should Not Use TDD 226
Chapter 9: Design Patterns and Idioms 227
Design Principles vs. Design Patterns 227
Some Patterns, and When to Use Them 228
Dependency Injection (DI) 228
The Singleton Anti-Pattern 229
Dependency Injection to the Rescue 232
Adapter 240
Strategy 241
Command 245
Command Processor 249
Composite 252
Observer 255
Factories 260
Simple Factory 260
Facade 263
Money Class 264
Special Case Object (Null Object) 267
What Is an Idiom? 270
Some Useful C++ Idioms 271
The Power of Immutability 271
Substitution Failure Is Not an Error (SFINAE) 272
The Copy-and-Swap Idiom 275
Pointer to Implementation (PIMPL) 278
Appendix A: Small UML Guide 282
Class Diagrams 282
Class 282
Interface 284
Association 287
Generalization 289
Dependency 290
Components 291
Stereotypes 292
Bibliography 293
Index 295

Erscheint lt. Verlag 27.9.2017
Zusatzinfo XVII, 291 p. 46 illus., 15 illus. in color.
Verlagsort Berkeley
Sprache englisch
Themenwelt Informatik Programmiersprachen / -werkzeuge C / C++
Mathematik / Informatik Informatik Software Entwicklung
Schlagworte Applications • C++ • clean • programming • Software
ISBN-10 1-4842-2793-X / 148422793X
ISBN-13 978-1-4842-2793-0 / 9781484227930
Haben Sie eine Frage zum Produkt?
PDFPDF (Wasserzeichen)
Größe: 5,5 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
Das umfassende Handbuch

von Torsten T. Will

eBook Download (2024)
Rheinwerk Computing (Verlag)
37,43
Das umfassende Handbuch

von Jürgen Wolf; René Krooß

eBook Download (2023)
Rheinwerk Computing (Verlag)
37,43
C++ lernen – professionell anwenden – Lösungen nutzen

von Ulrich Breymann

eBook Download (2023)
Carl Hanser Verlag GmbH & Co. KG
49,99