Review of Scott Meyers’ Effective Modern C++

The programming language, C++ has a long history in digital computer programming and information technology. It is the chief language for video games, most major desktop software (MS Office, Adobe, and AutoCAD), biotech analysis, aircraft systems, and certain kinds of information technology infrastructure. The programming language, C is more versatile and the chief language for creating operating systems like Windows, Mac OS X, and Linux and C++ allows you to harness C language capabilities in a more extensively modular way.

C++ in Context

Not everyone uses C++. It is not widely used in IT or in the production of general business software. These are situations where the languages Java, PHP, C#, Python, and others gives you both good structure and lower granularity. Low technical granularity is useful when it allows you to express logical concepts in code and the intent is more easily attained in execution. As a matter of course, this quality is highly practical concerning those areas of business and technology that benefit from tools that exhibit these qualities. In other situations, a tool like C++ that has the potential to provide excellent high level structure but seamlessly access the underlying hardware directly when needed and refine how memory is used and accessed can be very useful.

Moving from C-Style C++ to C++11 and C++14

C++ was originally based on C in terms of syntax. The gateway to the computer in practical terms is the syntax and conventions of the language used to define source code translated through compilers into the machine instructions that is the program. The way the syntax is construed and understood is related to understanding the meaning and implications of language conventions in a given program design. The C++ rules of writing has evolved from the C++98 standard that was somewhat like how to take some of the conventions of C and refine them strong program structure and modularity. A stronger relationship existed between C and C++98. Now, a different set of standards for C++ are available.

The standards known as C++11 and C++14 advances the syntax, conventions, style, capabilities and rules of C++. Several useful things are added to the language and those additions, revisions, and enhancements are well described in books by Bjarne Stroustrup and Nicolai Josuttis. Their books are deeply informative, necessary, and broadly covers the updates to the C++ language. Reading their books, you learn many insights for writing software better and integrating part of their thought process into your thinking. You will learn the C++ language from their materials if you did not previously know it as well as the suggested C++ mechanisms to use. What remains to understand is a wider set of recommended practices and things to consider regarding the updated C++ mechanisms.

Scott Meyers has written an updated version of his Effective C++ book covering C++11 and C++14. The primary benefit of Mr. Meyers book is to improve your use of the revised C++ by introducing many of the right choices and advice regarding its use. Perhaps a lack of knowledge of his material means a higher susceptibility to revert to C++98 style programming. I do not think C++98 approaches are inappropriate in many cases but there are alternatives in C++11 and C++14 that can improve upon things when those conventions apply. By the time I finished the book on 12/4/2014, I was surprised at the conclusions I drew.

Lessons from Scott Meyers Book

I wrote 7 pages of notes as I followed along the book. I rarely make notes as I read, but this book encourages it in the significance of what it reveals. While I read the book, I began to draw conclusions in a given chapter or recommendation section only to have a later chapter or section clarify the information such that I had to revisit the conclusions. You have to take the book as a whole. Flipping through quickly and zeroing in on an item will not work because the book builds information progressively from beginning to end. Many of the books on C++ are written in this way but the title of this book may connote a more flexible interaction with the material. You have to read it beginning to end the first time around. Rather than rehash 7 pages of notes, I just have a summary.

The first lesson I learned is that the conventions introduced in C++11 and C++14 are excellent, but they are not perfect. It means that things like auto type deduction does not always work, parenthetical expressions can change the meaning of a statement from an lvalue to an rvalue without you realizing it, move semantics can introduce great trouble if you are not careful, and there are many such gotchas that when you code C++, whether C++98, C++11, and C++14 the first lesson is do not look to C++ for automation but for precision. If you are willing to write code in such a way that you specify the program in a precise way rather than a much more general way, C++ is equal to the task.

The second lesson is that the new structures and conventions possible in C++ does not eliminate undefined behavior that could be an entry vector for malware and spyware. This can include but is not limited to type deduction, concurrency, and lambda expressions. The good news is that Scott Meyers has presented several sources of undefined behavior related to some of the new features of C++ and a reading of his book will raise your awareness of recommended ways to use certain aspects of C++11 and C++14 in this regard.

He does offer tips for conducting analysis of when to use certain features. I appreciated that he offered that what he has outlined are suggestions and not rules. The relevance of his observations still comes down to your best judgement as to what will work in the software you are writing. Take a feature like auto type deduced variables. You can use it excessively or use it strategically. Strategically, it means you can use auto deduced variables and parameters to streamline portability but not obscure the type, thus nature, of variables upstream and downstream from such variables.

The third lesson is that an analysis of features can be valuable. Some recommendations may be down right unappealing in their form but they still work to maintain code correctness. An example is overload resolution dealing with universal references. It is one thing to read a description of a language feature such as template metaprogramming and the value of it. It is another thing altogether to understand how template metaprogramming may work in actual practice and what you may want to observe in using such a feature. A careful analysis such as what is expressed in this book is what makes it highly valuable material.

A fourth lesson is that you are not going to get full automatic generality in C++ compared to C# or Java or something. What it means is that you have great expressivity in C++ but when you use C++, you have to be in control and take little for granted. Think more when you use C++ but the gains are what was stated earlier. This book shows there are choices in C++ such as the coverage of concurrency in which you can take a task based approach or one that is thread based. Scott Meyers presents excellent arguments for when to use each, the pitfalls, and severe cautions if you would avoid surprises in program termination or undefined behavior that can corrupt data or produce an entry vector for malware. It goes to a need for higher selectivity in the choice of features used in a C++ program.

A fifth lesson is that C++11 and C++14 does not greatly reduce the tradeoff between expressivity and performance. Mechanisms such as move semantics can help with performance and they may not. Scott Meyers makes that clear. What I found interesting was the idea that under certain conditions, improper control over move semantics could create undefined behavior, or lower performance, or increase time spent troubleshooting the behavior of the software. I liked the phrase Scott Meyers used about taking C++98 code and if you recompiled it on a C++11 or C++14 compiler, the entire program could just speed up. I think that is a broad assumption but more likely true than otherwise. Still, anyone who considers the impact of move semantics and universal references may want to review Bjarne Stroustrup’s materials on the specification of move constructors, move assignment operators, and so on to better grasp the mechanisms for controlling their use.

Conclusion

This closes a span of research for me that began in 2012 to update my core understanding of C++. At least for now. I started with a C++98 perspective that I grew from my research of C. I was quite able with 4 or 6 other programming languages besides these but I sought to know C and C++ more in order to have them available to me for the kinds of things I wanted to research or pursue independently. During this time, C++ changed and I decided it was worthwhile to learn these changes, read about best practices and evolve my knowledge structures related to these languages and tangential materials. Without Scott Meyers book, I faced many months of trial and error without an effective guide. Thankfully, due to his experience, I can broaden my insight to make better choices and follow a much better process of analysis when applying the newer versions of C++. I will continue to expand my understanding of C++ as a private matter and I feel better equipped to absorb future information on the topic and write code better when I find the inclination to create.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s