C++ for dinosaurs
This book is 100% complete
Completed on 2014-07-25
About the Book
There are certain programming habits that tend to produce unreadable and low-performance code. These habits are inherited from dated code, or passed on from trainer to trainee.
Readability should be the prime aspect of your code, because it has the highest return on effort, compared to other techniques which aim to improve your product's quality.
- Quality: Bugs are found mostly by reviewing other people's code. You can't review somebody else's code if you cannot read it. Bugs are not found by unit-tests, because unit-tests are created to capture errors that we know they exist in the code. Unit-tests only catch bugs that have been re-introduced.
- Efficiency: Maintenance takes about 80% of developers' time. Therefore, spending some time for writing better code will save you up a lot of time when you have to maintain it.
- Performance: Unreadable code is difficult to reason about. Therefore any opportunities for optimisation that may exist are often impossible to notice and fix.
I describe six basic techniques that you can follow in order to eliminate these habits. These techniques can be learned one at a time, improving your coding level progressively. At the end, you will have developed a programming style that
- is more readable,
- leads to more maintainable code,
- promotes reusability, and
- often leads to faster code.
All these qualities are not mutually exclusive, you will often achieve all of them at the same time.
The techniques are easy and therefore
- students can apply them
- it helps smooth transition from C to C++, without having to change programming paradigm
- it allows C++ programmers to write using native idioms, instead of writing like C, Java, or Fortran.
C++ for dinosaurs is also available as a printed book at lulu.com.
- Who is this book for
- How to read this book
- Why this book was written
1 Understanding why a style change is needed
- 1.1 Elements of wasteful programming style
- 1.2 Why is C++ a better choice than C for most cases ?
- 1.3 Why people don’t switch to C++ ?
- 1.4 Object oriented approach critique
2 Steps for embracing a better coding style
- 2.1 Step 1: Use readable syntax for function arguments
- 2.2 Step 2: Use the STL containers
- 2.3 Step 3: Do not write raw for-loops
- 2.4 Step 4: Algorithms should read like pseudocode
- 2.5 Step 5: Make use of the roll/unroll behaviour
- 2.6 Step 6: Revamp your callback methods
3 Efficiency advantages using STL and C++
- 3.1 Templates are faster than macros
- 3.2 Efficiency of std::string vs char*
- 3.3 Order of complexity does not reflect performance
- 3.4 Old speed issues that don’t exist anymore
- 3.5 map vs unordered_map
4 Am I doing it right ?
- 4.1 bind-mania
- 4.2 loving low-level data structures
- 4.3 for_each everywhere
- 4.4 replicating STL algorithms
- 1 Understanding why a style change is needed
Learning the details
5 Expressing intent using STL algorithms
- 5.1 Customising algorithms with comparators
- 5.2 Customising algorithms with predicates
- 5.3 Copying algorithms
- 5.4 How are algorithms implemented ?
- 5.5 What is the fastest way to learn STL algorithms ?
6 Callable Objects
- 6.1 Using functions as callable objects
- 6.2 Using function objects as callable objects
std::bind()to create callable objects
- 6.4 Lambda functions as callable objects
- 6.5 When to use each type callable functions:
7 Avoid using low-level strings
- 7.1 Problem description
- 7.2 The solution
8 Understanding cache issues
- 8.1 Running costs of a program
- 8.2 Data access costs
- 8.3 Costs depending on data structures
- 8.4 Instruction Pipeline
- 8.5 Side-stories
9 Converting a .c module into a .cpp
- 9.1 Ensure header is readable by C++
- 9.2 Mark external functions as extern “C”
- 9.3 Replace implicit casts from void *
- 10 References
- 5 Expressing intent using STL algorithms
The Leanpub 45-day 100% Happiness Guarantee
Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Free Updates. Free App. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets), MOBI (for Kindle) and in the free Leanpub App (for Mac, Windows, iOS and Android). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.