Code in books

Errors can be frustrating for authors too

In Code in Books, Bradford talks about his frustration about source code errors within technical books.

As far as code errors in books goes this is a very trivial error. But it does point to the fact that the person who wrote the example, in this case not the primary author, didn’t even enter the code in an IDE much less compiled or tested it.

I also find this frustrating and it's up to the author to ensure that they don't make any changes to code examples after cutting and pasting them from their IDE (assuming they do this, of course).

However, as an author myself and from my own experience, it's not necessarily the author that makes these mistakes. After spending around 8 months of my own time on Professional JSP Tag Libraries (Wrox, 2002), I was pretty gutted to see that so many mistakes had crept in between the final edits I saw and the copy that went into print. These included errors in the code samples along with errors in the UML diagrams that I had put together to describe the way that the tag library APIs worked. You see, all of my diagrams were redrawn before the manuscript went into print and I didn't get to see them beforehand. UML class and activity diagrams were redrawn incorrectly with trivial mistakes such as incorrect package and method names. You can see my old errata page here.

My recommendation to anybody writing books and articles? Make sure you get to see the final final final copy of the manuscript before it goes into print. Either that or self-publish via a blog. That way, at least you know the mistakes are yours. ;-)

p.s. I'd be really interested to hear from other authors about how you manage source code examples in books, articles and presentations. Do you cut and paste it from your IDE or do you have a much more sophisticated way of linking together text and source code?



Re: Code in books

Not exactly books, but when designing training courses, we used to represent the course information in an XML format, which was then translated to HTML (for on-line viewing) and LaTeX for printing (via latexpdf). The examples were actually live .java files that were weaved into the text during one of the XSLT steps in this process, so we always used 'live' code rather than adding it in. Of course, that works fine for mini examples, but doesn't work so well when you refer to it in the text (e.g. the public static void mini method). Still, can't be helped :-)

Re: Code in books

Alex's comment is interesting, because I believe that is pretty much how Andy Hunt and Dave Thomas included the code snippets in their venerable book, The Pragmatic Programmer.

Re: Code in books

Sounds interesting - how do you (a) author the XML and (b) include style information? Is there a standard XML format for this?

Re: Code in books

I recall in my college years discussion of Knuth developing a procedural language which was written in an inverted manner than "normal" code in terms of comments and code.

Comments were not <span class="me">delineated </span>but code was <span class="me">delineated </span>with markers.  The idea of the language was to write information in plain English and and intersperse your writing with code snippets.  LaTex was involved in some way (maybe in compiling the book form of the code?).  The compiler would pick up the code and compile that into executable form.

Javadoc appears to heavily use this concept.  Maybe a good idea would be the development of an extended Javadoc format for authors?

Re: Code in books

It's possible that you're thinking of TeX (rather than LaTeX, which is built upon TeX) and the language which was WEB. This was a mix of both documentation and code, and was processed with Web2C for compilation and Weave, which translated the Web comments into TeX documentation for printing. Anyhoo, it's not the tags that are relevant; any markup could be used. The problem is that if you have code snippets in any unnatural location (e.g. inside an HTML/LaTeX/Web document) that aren't processed by a compiler, then you may get trivial compile-time errors in the documentation. JavaDoc is no different in that regard; any Java code you put into JavaDoc (e.g. in pre blocks explaining how to use a library) may suffer from the traditional kind of typing errors. For example, if you mis-typed 'for' as 'fpr' in a JavaDoc comment, it wouldn't raise any errors even though someone typing that same example into a Java program would see them. The key is to extract the code from known working/compiled programs, rather than have them represented as text. It's fairly easily done with simple CDATA type wrappers at the beginning/end of a file and then judicious use of the XSLT's document() function.

Add a comment Send a TrackBack