Tuesday, January 29, 2013

Effective C++11: Background

I've mentioned in some earlier posts that I plan to start writing a new book, Effective C++11.  The purpose of this post is to tell you a little bit about it. Lest there be confusion, let me emphasize that there is no book yet. If everything falls into place the way I hope it will, there will be a book about 10 months from now. If. I'm not making any promises.

If you're looking for information on the content of Effective C+11, this is the wrong place to seek it. I had originally planned to include a content summary, but once I'd written the background information that follows, I realized that the post was already too long. So the content information will go in a separate post.

People have been asking me to write Effective C++11 since before it would have been possible to give it that title. I started getting requests when the new version of the language, then known as C++0x, was still under development. I had a ready reply for such requests, aside from my considerable reluctance to write a book about a language that was still in flux. (Poor Anthony Williams' C++ Concurrency in Action remained in publishing purgatory for nearly three years, because he had the misfortune to publish an "early access" digital draft two years before the C++ Standard it corresponded to settled down. As a result, he had to keep revising the draft as updates to the emerging Standard were made, and that cannot have been fun. The result was worth both his effort and our patience, because the book is excellent. I strongly encourage you to consider buying it. I have. But I'm really glad I didn't have to go through the ordeal of writing it.)

My ready reply to requests to whip out Effective C++Whatever was that it's not possible to describe how to use a technology effectively until the community has enough experience with the technology to know what truly works and what doesn't. We'd all like to skip over experiencing the disappointment of what seem like great ideas that fail to pan out. We'd like to instantly come up with the nifty applications of features in ways nobody had foreseen. That'd be great. But as far as I know, the only way to separate the effective uses of C++11 from the ineffective ones is to try as many things as we can, then see what ends up in which pile. That takes time.

In truth, most people didn't ask for Effective C++Whatever. They asked for a new edition of Effective C++. It was the obvious thing to ask for, and it was the obvious thing for me to write, but it presented me with a problem I didn't know how to overcome.

The problem was (and is) that I constrain myself to books with no more than 300 pages. There is some wiggle room in that constraint, because I don't count front matter (preface and tables of contents, etc.--generally stuff numbered using Roman numerals), and if I had to, I'd probably be willing to ignore pages comprising the index. Nevertheless, my goal is to avoid producing a book with a page number higher than 300. The current edition of Effective C++ (available at fine bookstores everywhere, not to mention at a variety of illegal download sites, sigh) has, if you ignore the appendices and the index, a maximum page number of 272. That doesn't leave a lot of room for new material. And, as I've blogged about elsewhere, the material that's currently in the book remains largely valid under C++11. I didn't want to jettison a lot of useful material that's as valid under C++11 as under C++98, but I'm serious about the advantages of comparatively short books, so I didn't want to exceed the 300-page barrier, either.

The page limit problem held me back at least as much as the lack-of-experience problem. I knew that time would address the experience issue, but 300 pages wasn't going to get any bigger, no matter how long I waited. (Yes, I think like a programmer, so I couldn't help but toy with the ideas of playing games with smaller font sizes, narrower margins, page numbering using bases other than 10, and eliminating page numbers entirely. But cheating's less fun when the rules you're evading are the ones you made up in the first place.)

Last year, I experienced the proverbial brainsquall, and I realized that if most of Effective C++ remains valid under C++11, the solution is to not write a new edition of that book, but to write a new book. A book devoted to making effective use of the features unique to C++11. A book to complement Effective C++. That's my plan for Effective C++11:  to write a book describing how to make effective use of the features and capabilities in C++11 that are not present in C++98. A book with content entirely separate from my other C++ books and also largely separate from the content of my C++11 Overview Materials. In other words, all new stuff.  New from me, anyway.

The page constraint was thus vanquished, and experience with C++11 was slowly piling up on the information superhighway, but I had another concern. When I wrote my other books, I knew how they'd be published: as one or two colors of ink on more or less white paper of a fixed size. But the future of publishing was clearly digital, and I didn't know what it meant to produce a manuscript that should ultimately look good and be useful as both ink on paper and pixels on a screen, especially when some screens were monochrome (e.g., Kindle), and screen sizes varied from those of smart phones to those of UHDTV-capable monitors. During 2008-2010, I spent some time agonizing over this issue in my blog for Fastware!, a book that never came to be. I even gave a talk at a publishing conference about my concerns.

To be honest, I'm still not sure how to approach writing a manuscript when the result is to be published on multiple platforms, but analysis paralysis loses its appeal after a while, so I finally decided that if I don't know what the right way is, I might as well take a guess and see what happens. Exactly what that will mean in practice, I'm not sure, but my current plan is to write using Microsoft Word using lots of styles, and my assumption is that whatever manuscript format looks to be "right" when I have a manuscript ready to go, somebody will be able to figure out how to translate what I have in Word into that format.

Okay, having slogged through my whining about lack of experience, page limits, and manuscript preparation, you'd like to think I'll finally do something with a payoff for you. Something like offer some details about what will be in the book.  That I will. In my next post, which I plan to start writing immediately after publishing this one. Stay tuned.



Tom said...

This is great news Scott, really pleased the book is finally under way! Can't wait to read it. Which arm can I offer to tech review it? :-)

Cory Riddell said...

For a minute there, I thought you were going to pull a Knuth and write a better publishing system so that you can write the books you want.

Francisco Lopes said...

I admire how Pro Git was able to attain good enough formatting in various platforms: http://git-scm.com/book. Good code formatting and illustrations, published even to html web page. I've seen other books that achieved similar success.
I own Williams book, physical, pdf and real ebook format, epub. Although a good book, source code in epub is displayed through pictures to preserve formatting. I really dislike this, and I think this is what someone may end up if proper attention is not taken from beginning. Best wishes.

Francisco Lopes said...

Text in pictures is bad for zooming and night mode in ereaders.

Scott Meyers said...

@Tom: Thanks for expressing interest in being a reviewer. While I appreciate the offer of body parts, cold hard cash is somewhat more practical. Except...damn. I don't take bribes. Sigh.


Scott Meyers said...

@Cory Riddell: I wish I were in Knuth's league, but I'm not. Also, I'd like to get the book out this year.


Scott Meyers said...

@Francisco Lopes: I agree that code should not be rendered as an image (as I mention here). The problem of how to offer "good" source code formatting across output devices is a topic of active discussion among me, my publisher, and other authors I know. I'd be grateful if you'd post books you think have done this well as a comment on my post regarding code in digital publications.


Zenju said...

> write using Microsoft Word

This sounds crazy! :) Why don't you use Latex? (this is coming from someone who was using MS word for large documents and who was told by others that he was crazy doing so). Latex let's you define your format at a single place and apply it to all places in your book that apply. It also solves your "multiple target platforms: book, pdf, html, ect." problem. I had thought you'd be using it already for your EC++ books!

>you'd post books you think have done this well

Just take the formatting of Herb Sutter's Exceptional C++ Series! I'm sure he don't mind :)

Klaim said...

Hi Scott, I just wanted to share some thought as I'm deeply interested in publishing stuffs, more in the comics/audio-visual contexts, but the problems are basically the same for books.
I'm right now watching your presentation about an agnostic format for publishing.

I understand the problem because I figured years ago that there is the same problem for digital comics (assuming they might or not, depending on the author wish and specificities of the work, be published on paper).

I'm working on a format that would be used as inter-operable representation of digital story-telling. This format assume that the story is a sequence, controlled by the reader, but ultimately just a sequence of changes applied to "something in the screen" (or audio or other media). It is not suitable for plain document like books, but I believe that it's the right thing for stories exploiting fully the digital platform (my work is open source).

So, I also have some informations about formats interesting for your case. I believe that the best bet would be the result of the work on the next versions of epub. Epub isn't perfect for now but might be suitable for your purpose once it is fixed and enhanced. Unfortunately, don't wait for it this year.

It's both sad and awesome news: there is no good inter-operable format (which might not be the final format but a format used to generate other format for different specific targets) so far. However, it means there is a big opportunity to build such format.

The problem I see is that most formats made to write something, being code or books etc. Are thought to be usable directly by the author as raw text, which limits a lot the inter-operability using tools to convert the format. I believe that the format itself shouldn't be made for humans to edit it, but for ease of writing tools and readers for it. That's what I do with my digital-story-telling format and that's what I think a format fixing your problem should be too.

Scott Meyers said...

@Zenju: I'm not enthusiastic about LateX, because, as I've written here, I think that markup-based approaches are inherently inferior to WYSIWYG approaches for authors.

Also, I want to leave all the detailed presentation decisions to my publisher. In the past, I've produced final PDF ("camera-ready copy") for my books, but when my book is going to be consumed on multiple devices with differing sizes and capabilities, I want to leave the porting work to people who do that for a living. They should worry about how to make my material look good and be most useful on Kindle and iPad and Surface and Samsung Galaxy n and, oh yes, ink on paper. I'd rather spend my time focusing on where to use curly brackets.

With LaTeX, I can do all that, of course, but I should be able to do what I need with Word, too, I think, and Word is a more pleasant authoring environment for me. If I were planning to produce final PDF for each platform, my decision might be different. But I'm not.


Francisco Lopes said...

I can't recall all the good ebooks I've read (but Pro Git is a good enough sample I guess). But I've the hard feeling none of those I've read and praised where built with Word, really. They look as the outcome of a proper typesetting system, LaTeX or maybe even markdown and pandoc.

Scott Meyers said...

@Francisco Lopes: I don't view Word as a way to produce the version of the book that's published, i.e., as a mechanism for producing what's displayed to readers. I view it as a tool for producing a manuscript that has the necessary information in it to permit that manuscript to be rendered well on multiple output devices. The idea is that I deliver Word to my publisher, and from that the publisher produces PDF or HTML or epub or whatever format the target device requires.

Think of Word as the concrete syntax accepted by a system that takes a file (the book's "source code") and translates it into a file (the book's "object code") that's optimized for a particular platform. With this model, the fact that I'm working in Word is immaterial to you as a reader. Just as you can't tell what text editor was used to produce C++ source code from the executable, there's no reason you should be able to determine if I used Word or Notepad+LaTex or something else from the book you ultimately read.

This is not breaking new ground, BTW. Most authors these days use Word to produce manuscripts that their publishers then typeset using something like InDesign. The fact that the document was originally written in Word (or whatever) is not apparent from the book that gets printed. Nor should it be.

My choice to use Word is based on its widespread support and familiarity in publishing circles, plus the fact that it's a capable document production system. (I said capable, not ideal :-}) . As I've written elsewhere, I figure that if I do a good job of using styles in Word to indicate logical markup, it should be possible for my publisher to translate what I produce into whatever they need.


Francisco Lopes said...

@Scott Meyers, I'm not totally convinced by that point, when I said that they look produced with a proper typesetting system, I talk of someone with familiarity with it and willing to have more control of the final form (like Pro Git looks).

By the source/object metaphor, there're times one is indeed able to look and know something was produced with bias for a given platform, and hence, in the given platform, and also one can guess just by looking at sources solely, it was written with a given toolset or not. Like in, "even thought multiplatform, this code is from a microsoft guy, look at this hungarian notation!".

I mean that, the background environment one thing is produced, often times reverberates in its form. And I meant of making inference and judgement (and wrong guesses) from this point of view.

Francisco Lopes said...

But, anyway, achieving good source code indentation (maybe with some highlighting), never inside images is enough to make one e-book reader more happy. These critical points being properly addressed at deploying on multiple targets, despite the toolset, is what matters. If it can be done in Word, let's do it in Word then.

Francisco Lopes said...

To my surprise (or not), I just that checked pandoc documentation gives Pro Git as an example for ebook creation with its system (http://johnmacfarlane.net/pandoc/epub.html). For me, this is a special, right in the spot, kind of guess, I was expecting Pro Git to only be built with something like that (given deployment to web page even), and just found it out to be true.

Francisco Lopes said...

I just checked another markdown/pandoc based book (produced in the open, I think there's nothing as modern as this): Developing Backbone.js Applications. It's still in-progress but already look awesome. I've downloaded the prebuilt epub from the repository and opened on chrome with magicscroll (with small fonts and default theme set). All I expect from good source code formatting is there, the only caveat seems it's still not built with night mode in mind (and by being produced in the open, he is able to be readily warned of that by his audience). Since the epub is able to look this good, the print and pdf can only be better (generally epub or other true ebook format is the least common factor in presentation goodness).

Francisco Lopes said...

Another promising one: Sample PDF Chapter;
Author's pandoc tip: Listing captions with delimited code blocks and Pandoc;
Author's experience with his publisher (same one as Pro Git) and self publishing: Lessons from self-publishing - Part I .

Scott Meyers said...

@ Francisco Lopes: Regarding Developing Backbone.js Applications, I just downloaded the epub for chapter 2 and viewed it in Firefox (using EPUBReader I agree that it looks nice, but the source code is blindly wrapped when the window isn't wide enough, and you need a pretty wide window to accommodate this on one line:

console.log("I should " + todo.get("title") + ". Have I done it before? " + (todo.get("completed") ? 'Yeah!': 'Not.' ));

I am unaware of any general automatic mechanism for wrapping code, so for the time being, I think that good code displays are dependent on proper care on the part of the author when s/he write the manuscript.


Francisco Lopes said...

@Scott Meyers, Sure, in truth I also disliked font size variation. The last book I mention (The logstash Book), seems to use some nice arrow for line continuation, that the author also use in his own blog.

Francisco Lopes said...

Just an unicode character (↩). He uses it at some long line logs in source code blocks at the free sample chapter.

James Turnbull said...

LaTeX using the Listings (or similar) packages is generally pretty good at auto-breaking lines and has a variety of options to configure the right bhavior (break on whitespace or not, etc). I use the following code to break the lines:

prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\rhookleftarrow}},

You need a font that contains the Unicode character.

Scott Meyers said...

@James Turnbull: Figuring out where to break a line is a challenge, but equally challenging is figuring out what the appropriate indentation of the wrapped part of the line is. And if what's being wrapped is a "//" comment or a string literal, simple wrapping will introduce a syntax error. My feeling is that if an author wants to maximize the likelihood that readers will see well-formatted code, that author has to take steps to minimize the likelihood that code displays will get wrapped by anybody other than the author.


James Turnbull said...


That's not always feasible but I take your point. Most of the code I am wrapping this is not an issue or I specifically handle those differently.