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

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

Clean C++: Sustainable Software Development Patterns and Best Practices with C++ 17
Автор: Roth Stephan
Дата выхода: 2017
Издательство: Apress Media, LLC.
Количество страниц: 299
Размер файла: 2,6 МБ
Тип файла: PDF
Добавил: codelibs
 Проверить на вирусы

Contents at a Glance
Contents
About the Author
About the Technical Reviewer
Acknowledgments
Chapter 1: Introduction
Software Entropy
Clean Code
Why C++?
C++11 – The Beginning of a New Era
Who This Book Is For
Conventions Used in This Book
Sidebars
Notes, Tips, and Warnings
Code Samples
Coding Style
Companion Website and Source Code Repository
UML Diagrams
Chapter 2: Build a Safety Net
The Need for Testing
Introduction into Testing
Unit Tests
What about QA?
Rules for Good Unit Tests
Test Code Quality
Unit Test Naming
Unit Test Independence
One Assertion per Test
Independent Initialization of Unit Test Environments
Exclude Getters and Setters
Exclude Third-Party Code
Exclude External Systems
And What Do We Do with the Database?
Don’t Mix Test Code with Production Code
Tests Must Run Fast
Test Doubles (Fake Objects)
Chapter 3: Be Principled
What Is a Principle?
KISS
YAGNI
DRY
Information Hiding
Strong Cohesion
Loose Coupling
Be Careful with Optimizations
Principle of Least Astonishment (PLA)
The Boy Scout Rule
Chapter 4: Basics of Clean C++
Good Names
Names Should Be Self-Explanatory
Use Names from the Domain
Choose Names at an Appropriate Level of Abstraction
Avoid Redundancy When Choosing a Name
Avoid Cryptic Abbreviations
Avoid Hungarian Notation and Prefixes
Avoid Using the Same Name for Different Purposes
Comments
Let the Code Tell a Story
Do Not Comment Obvious Things
Don’t Disable Code with Comments
Don’t Write Block Comments
Don’t Use Comments to Substitute Version Control
The Rare Cases Where Comments Are Useful
Documentation Generation from Source Code
Functions
One Thing, No More!
Let Them Be Small
“But the Call Time Overhead!”
Function Naming
Use Intention-Revealing Names
Arguments and Return Values
Number of Arguments
Avoid Flag Arguments
Avoid Output Arguments
Don’t Pass or Return 0 (NULL, nullptr)
Strategies to Avoid Regular Pointers
Prefer simple object construction on the stack instead of on the heap
In a function’s argument list, use (const) references instead of pointers
If it is inevitable to deal with a pointer to a resource, use a smart one
If an API returns a raw pointer…
The Power of const correctness
About Old C-style in C++ Projects
Prefer C++ Strings and Streams over Old C-Style char*
Avoid Using printf(), sprintf(), gets(), etc.
Prefer Standard Library Containers over Simple C-style Arrays
Use C++ casts Instead of Old C-Style Casts
Avoid Macros
Chapter 5: Advanced Concepts of Modern C++
Managing Resources
Resource Acquisition Is Initialization (RAII)
Smart Pointers
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
Managing Proprietary Resources
We Like to Move It
What Are Move Semantics?
The Matter with Those lvalues and rvalues
rvalue References
Don’t Enforce Move Everywhere
The Rule of Zero
The Compiler Is Your Colleague
Automatic Type Deduction
Computations during Compile Time
Variable Templates
Don’t Allow Undefined Behavior
Type-Rich Programming
Know Your Libraries
Take Advantage of <algorithm>
Easier Parallelization of Algorithms Since C++17
Sorting and Output of a Container
Comparing Two Sequences
Take Advantage of Boost
More Libraries That You Should Know About
Proper Exception and Error Handling
Prevention Is Better Than Aftercare
An Exception Is an Exception – Literally!
If You Can’t Recover, Get Out Quickly
Define User-Specific Exception Types
Throw by Value, Catch by const Reference
Pay Attention to the Correct Order of Catch-Clauses
Chapter 6: Object Orientation
Object-Oriented Thinking
Abstraction – the Key to Master Complexity
Principles for Good Class Design
Keep Classes Small
Single Responsibility Principle (SRP)
Open-Closed Principle (OCP)
Liskov Substitution Principle (LSP)
The Square-Rectangle Dilemma
Favor Composition over Inheritance
Interface Segregation Principle (ISP)
Acyclic Dependency Principle
Dependency Inversion Principle (DIP)
Don’t Talk to Strangers (Law of Demeter)
Avoid Anemic Classes
Tell, Don’t Ask!
Avoid Static Class Members
Chapter 7: Functional Programming
What Is Functional Programming?
What Is a Function?
Pure vs. Impure Functions
Functional Programming in Modern C++
Functional Programming with C++ Templates
Function-Like Objects (Functors)
Generator
Unary Function
Predicates
Binary Functors
Binders and Function Wrappers
Lambda Expressions
Generic Lambda Expressions (C++14)
Higher-Order Functions
Map, Filter, and Reduce
Map
Filter
Reduce (Fold)
Fold Expressions in C++17
Clean Code in Functional Programming
Chapter 8: Test-Driven Development
The Drawbacks of Plain Old Unit Testing (POUT)
Test-Driven Development as a Game Changer
The Workflow of TDD
TDD by Example: The Roman Numerals Code Kata
Preparations
The First Test
The Second Test
The Third Test and the Tidying Afterwards
More Sophisticated Tests with a Custom Assertion
It’s Time to Clean Up Again
Approaching the Finish Line
Done!
The Advantages of TDD
When We Should Not Use TDD
Chapter 9: Design Patterns and Idioms
Design Principles vs. Design Patterns
Some Patterns, and When to Use Them
Dependency Injection (DI)
The Singleton Anti-Pattern
Dependency Injection to the Rescue
Adapter
Strategy
Command
Command Processor
Composite
Observer
Factories
Simple Factory
Facade
Money Class
Special Case Object (Null Object)
What Is an Idiom?
Some Useful C++ Idioms
The Power of Immutability
Substitution Failure Is Not an Error (SFINAE)
The Copy-and-Swap Idiom
Pointer to Implementation (PIMPL)
Appendix A: Small UML Guide
Class Diagrams
Class
Interface
Association
Generalization
Dependency
Components
Stereotypes
Bibliography
Index

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 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. 


Похожее:

Список отзывов:

Нет отзывов к книге.