Modern C++ Programming with Test-Driven Development
Code Better, Sleep Better
by Jeff Langr
Version: P1.0 (October 2013)
Copyright 2013 The Pragmatic Programmers, LLC. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copywith other members of your team, with friends, or via file sharing services. Thanks.
Dave & Andy.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Table of Contents
Copyright 2013, The Pragmatic Bookshelf.
Early Praise for Modern C++Programming with Test-Driven Development
Jeff Langr has written another excellent book. This time he brings Test-Driven Development to the world of C++. Jeffs starting example brings us face to face with the stark simplicity of good TDD. He explains why we work that way and then provides key practical details, all the way through test doubles, working with legacy code, threading, and more. This one is a keeper for anyone working with C++!
Ron Jeffries |
Co-creator of the Extreme Programming methodology |
Jeff Langr has written the best C++ book in years. Modern C++Programming with Test-Driven Development is the perfect mixture oftheory and practice. The abstractions are explained with clarity andgusto, but the details are right there when you need them. Its sure to be an instant classic, in C++ and in TDD both.
Michael D. Hill |
XP coach and writer |
Jeff is an expert software craftsman, and in this book he shares his considerable wisdom about crafting great software. This book is not about testing, though you will learn valuable testing techniques. It is about improving your skills, code, products, and life through test-driving. Whether youre a novice, expert, or in between, Jeff will show you the how and the why of an expert who test-drives better products with C++.
James W. Grenning |
Author of Test-Driven Development for Embedded C |
Foreword
Dont let the title mislead you.
I mean, here is a really, really good book about design principles, coding practices, Test-Driven Development, and craftsmanship, and they go and give it a title like Modern C++ Programming with Test-Driven Development . Sigh.
Oh, dont get me wrong. This is a book about modern C++ programming. I mean, if you are a C++ programmer, youre going to love all the code in this book. Its just filled to the brim with really interesting and well-written C++ code. In fact, I think there may be more code than words. Go ahead, thumb through the book. Do you see a page without code on it? Not many I bet! So if youre looking for a good book to teach you modern practices of C++, by example after example after example, then youve got the right book in your hands!
But this book is about a lot more than just modern C++ programming. A lot more. First, this book may be the most complete and accessible exposition on Test-Driven Development that Ive seen (and Ive seen a lot!). Virtually every TDD issue weve uncovered in the last decade and a half is talked about in these pages, from fragile tests to mocks, from the London school to the Cleveland school, and from Single Assert to Given-When-Then. Its all here, plus a whole lot more. Moreover, its not some academic compendium of disconnected issues. No, this book walks through the issues in the context of examples and case studies. It shows the problems and the solutions in code .
Do you need to be a C++ programmer to understand it? Of course you dont. The C++ code is so clean and is written so well and the concepts are so clear that any Java, C#, C, or even Ruby programmer will have no trouble at all.
And then there are the design principles! For goodness sake, this book is a design tutorial! It takes you on a step-by-step voyage through principle after principle, issue after issue, and technique after technique. From the Single Responsibility Principle to the Dependency Inversion Principle, from the Interface Segregation Principle to the Agile principles of simple design, from DRY to Tell-Dont-Askthis book is a gold mine of software design ideas and solutions. And again, these ideas are presented in the context of real problems and real solutions in real code.
And then there are the coding practices and techniques. This book is just chock-full of them, from small methods to pair programming and from coding katas to variable names. Not only is there a ton of code from which to glean all these good practices and techniques, but the author drives each point home with just the right amount of discussion and elaboration.
No, the title of this book is all wrong. Its not a book about C++ programming. Its a book about good software craftsmanship that just happens to use C++ as the language for its examples. The name of this book should really be Software Craftsmanship: With Examples in Modern C++ .
So if you are a Java programmer, if you are a C# programmer, if you are a Ruby, Python, PHP, VB, or even a COBOL programmer, you want to read this book. Dont let the C++ on the cover scare you. Read the book anyway. And while you are at it, read the code. You wont find it hard to understand. And while you are learning good design principles, coding techniques, craftsmanship, and Test-Driven Development, you might also discover that a little C++ never hurt anybody.
Uncle Bob Martin
Founder, Object Mentor Inc.
Copyright 2013, The Pragmatic Bookshelf.
Introduction
Despite the current explosion in programming languages, C++ soldiers on. It is the fourth-most popular programming language, per the July 2013 Tiobe index. (You can find the latest index at http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html .) The 2011 ISO standard (ISO/IEC 14822:2011, aka C++11) brings features to C++ that may increase its acceptance...or at least soften objections against its use.
C++ remains one of your best choices for building high-performance solutions. If your companys products integrate with hardware, chances are you have a sizeable existing system built in C++. If your company has been around since the 1990s or earlier, chances are you have a long-lived C++ system, and chances are also good that its not disappearing anytime in the next several years.
Given that youre now working in C++, you might be thinking one of the following things: