Title: Len Dorfman's C++ By Example
Authors: Len Dorfman
Publisher: McGraw-Hill Object
Technology Series
Pages: 300
Price: US$39.95, disk included
ISBN: 0-07-911954-9
Unless you're perfect, or a masochist, it's probably best not to read a new programming book right after you've pushed a project out the door. Each twist of spaghetti code is fresh in your mind, and each of the author's sparkling clean examples only serve to deepen the post-partem depression. If it's a good book, then at some point, one of the author's explanations will cross paths with one of your freshly made mistakes and you'll say to yourself, "Damn, shoulda done it that way." For me, that never happened with Len Dorfman's C++ By Example. That's not to say there's anything wrong with the code; rather, it's the book's presentation of that code that fails to inspire either remorse or excitement.
This book is a presentation of the author's C++ course, aimed at MS-DOS programmers with a couple of years of C experience. You can divide the book into roughly two parts: text; and the example code, in which Dorfman develops a DOS character-mode user interface class library. The text sections, chapters 1, 2, and 4, present a good, brief introduction to object-oriented analysis and design. Chapters 1 and 2 provide a problem statement and the structured-query method, by which the Dorfman begins to solve the problem. Chapter 4 describes the evolution of the class library design as it progressed toward its final form.
The "by example" in the title is not an exaggeration. The entire remainder of the book is code. (In case you're wondering what happened to Chapter 3, that's all code as well.) By my conservative estimate, the book is at least 70% source code. As an example, the chapter presenting the Window class, perhaps the key class in a UI class hierarchy, consists of 36 pages with only six paragraphs of text four for introduction and two for summary. The classes (Message, Event, Window, Dialog, MenuBar) will be familiar to anyone who's used any of the current crop of class libraries, (OWL, MFC, etc.). Within the limitations of character-based DOS, they're all here. If you haven't used something like MFC, Dorfman's class library would provide valuable preparation for that more arduous pursuit.
The example code, 1,700 lines (including whitespace and comments), is included on a bundled diskette. The code is written for Borland C++ for DOS (which, of course, I don't use); porting to Watcom 10.0 was relatively painless, with all but the last two programs taking only a couple of hours total.
There is no discussion of syntax anywhere in the code listings. You need to have written at least some C and probably some C++ to benefit from this book. This code-intensive approach seems best suited to programmers who have written some C programs under DOS and want to jump into C++ without doing a lot of reading. Since the book's sample application is written around a user interface, it presents issues familiar to anyone who has tried to write a decent-looking, DOS character mode program.
The example code is the book's strength, but it is also its biggest weakness. One of the great strengths of well-written object-oriented code is that the objects "look like" things in the real world and are thus much easier to understand. The best intro to OOP I ever read used an example based on blackjack, with easily accessible objects such as cards, decks, and games. The book's use of nerdly objects, such as mice, monitors, and keyboards, needlessly throws away that advantage.
Implementing a DOS user interface creates another side effect in the book's code a complete dependence on BIOS calls. This BIOS dependence fills the code with stuff like union REGS ir; ir.h.ah=0x03; int86(0x10,&ir,&or); (Everyone who finds this readable and enlightening, raise your hand.) To my mind, these BIOS calls are not only ugly and unneccessary, they also detract from the book's stated goal of teaching C++, not MS-DOS.
Though this last criticism is heartfelt, I don't want it to overshadow everything else; for what it is, the book is very well done. Having chosen this particular problem, Dorfman presents a laudably pragmatic and complete solution. His classes are logical, simple, and clean. They also work, which is something you can't always assume with code you find in books and magazines. If I needed to write a DOS program with a minimal UI, I wouldn't hesitate to use this code.
An Admirable Attitude
Dorfman is pragmatic to a fault, dwelling at length on a couple of situations where he threw OO purity out the window in order to make his program work. Rather than anticipating problems, Dorfman works around them as they occur. He doesn't gloss over the essentially messy nature of programming either. At one point, for example, he shows seven different cuts at one object hierarchy. He is never dogmatic and readily admits that doing it some other way would work (an admirable personal quality that does not occur naturally in programmers). His style is persistently candid and relaxed and you can almost picture him standing next to the overhead projector adjusting the transparency.
I hate to pan this book, because the example code is well-written and works. If a subordinate turned this code in as work product, you'd pat him on the back and give him 1,000 extra stock options at bonus time. Unfortunately, as a learning tool, I have to rate C++ By Example as a bad deal. This is a harsh statement, but not unreasonable; since the book is mostly source code it must compete against volumes of readable, high-quality C++ code that can be had for free. Had Dorfman included a number of well-thought-out exercises, emphasizing the use of his class library at the end of each chapter, the book might become the basis for a hands-on, continuing-ed C++ course, which is in fact where it originated. As it stands, though, it reads more like a long magazine article inflated to book length by whitespace and BIOS calls.
John Rodley is an independent consultant living in Scituate, Mass. Holder of a BS in Computer Science, his current interests include the Java language, multiprocessing, operating systems in general, and OS/2 and UnixWare in particular. He can be reached at john.rodley@channel1.com or check out his home-page at http://www.channel1.com/users/ajrodley/.